Need Thread To Block Next Code Line But Not Other Threads
I have a method that uses a thread to connect to my database to check if the user exists on the database. I have another thread that is responsible to update the GUI. I need the database thread to block the code that comes after it until it terminates, but not to block the GUI thread. How do I manage to do that?
I've Tried using the join command, but it blocks the GUI thread. And I've tried using a loop that does nothing until the database thread is not alive, but it blocks too.
#GUI Thread
self.guiThread = threading.Thread(group= None, target=self.guiLoop)
self.guiThread.start()
#GUI Method
def guiLoop(self):
while True:
self.Update()
#Login Button Method
def onLoginButtonClick(self, event):
id = self.idBox.GetValue()
self.idBox.SetValue("")
password = self.passwordBox.GetValue()
self.passwordBox.SetValue("")
self.retList =
self.testBool = False
dbThread = threading.Thread(group= None, target=self.checkUser, args=(id, password, self.retList))
dbThread.start()
#TODO Blocking
print (self.retList)
#Database Thread Method
def checkUser(self, id, password, retList):
self.retList = DB.checkUser(id,password)
DB.checkUser is the method that checks the database if it has the user, and returns a list with user information, or an empty list if it does not have the user on the database.
python multithreading blocking
|
show 1 more comment
I have a method that uses a thread to connect to my database to check if the user exists on the database. I have another thread that is responsible to update the GUI. I need the database thread to block the code that comes after it until it terminates, but not to block the GUI thread. How do I manage to do that?
I've Tried using the join command, but it blocks the GUI thread. And I've tried using a loop that does nothing until the database thread is not alive, but it blocks too.
#GUI Thread
self.guiThread = threading.Thread(group= None, target=self.guiLoop)
self.guiThread.start()
#GUI Method
def guiLoop(self):
while True:
self.Update()
#Login Button Method
def onLoginButtonClick(self, event):
id = self.idBox.GetValue()
self.idBox.SetValue("")
password = self.passwordBox.GetValue()
self.passwordBox.SetValue("")
self.retList =
self.testBool = False
dbThread = threading.Thread(group= None, target=self.checkUser, args=(id, password, self.retList))
dbThread.start()
#TODO Blocking
print (self.retList)
#Database Thread Method
def checkUser(self, id, password, retList):
self.retList = DB.checkUser(id,password)
DB.checkUser is the method that checks the database if it has the user, and returns a list with user information, or an empty list if it does not have the user on the database.
python multithreading blocking
2
What code are you talking about when you say, "the code that comes after it."?
– Solomon Slow
Jan 2 at 1:05
2
When you talk about a mutex, don't talk about what code it protects. Code is read-only. Code does not need protection. You should be talking about what data are protected by the mutex instead.
– Solomon Slow
Jan 2 at 1:08
You want to blockprint (self.retList)
? Put it inside the thread and just return afterdbThread.start()
.
– zvone
Jan 2 at 1:18
But I need the rest of the code to not run until it finishes the action. I need all code to be blocked (the print(self.retlist) is just an example for the code that needs to be blocked, I need the code outside of the method to run only after an answer from the database was recieved) but I do need the gui thread to run so the gui would not freeze. Is there a way?
– RoBaMe
Jan 2 at 9:12
It's beginning to sound as if what you really want is for some GUI widgets to be disabled or inaccessible until the background task is finished. Why not haveonLoginButtonClick(...)
put the GUI into a mostly disabled state, havecheckUser(...)
post an event when it is finished, and have the event handler reenable all of the commands that previously were disabled?
– Solomon Slow
Jan 2 at 11:01
|
show 1 more comment
I have a method that uses a thread to connect to my database to check if the user exists on the database. I have another thread that is responsible to update the GUI. I need the database thread to block the code that comes after it until it terminates, but not to block the GUI thread. How do I manage to do that?
I've Tried using the join command, but it blocks the GUI thread. And I've tried using a loop that does nothing until the database thread is not alive, but it blocks too.
#GUI Thread
self.guiThread = threading.Thread(group= None, target=self.guiLoop)
self.guiThread.start()
#GUI Method
def guiLoop(self):
while True:
self.Update()
#Login Button Method
def onLoginButtonClick(self, event):
id = self.idBox.GetValue()
self.idBox.SetValue("")
password = self.passwordBox.GetValue()
self.passwordBox.SetValue("")
self.retList =
self.testBool = False
dbThread = threading.Thread(group= None, target=self.checkUser, args=(id, password, self.retList))
dbThread.start()
#TODO Blocking
print (self.retList)
#Database Thread Method
def checkUser(self, id, password, retList):
self.retList = DB.checkUser(id,password)
DB.checkUser is the method that checks the database if it has the user, and returns a list with user information, or an empty list if it does not have the user on the database.
python multithreading blocking
I have a method that uses a thread to connect to my database to check if the user exists on the database. I have another thread that is responsible to update the GUI. I need the database thread to block the code that comes after it until it terminates, but not to block the GUI thread. How do I manage to do that?
I've Tried using the join command, but it blocks the GUI thread. And I've tried using a loop that does nothing until the database thread is not alive, but it blocks too.
#GUI Thread
self.guiThread = threading.Thread(group= None, target=self.guiLoop)
self.guiThread.start()
#GUI Method
def guiLoop(self):
while True:
self.Update()
#Login Button Method
def onLoginButtonClick(self, event):
id = self.idBox.GetValue()
self.idBox.SetValue("")
password = self.passwordBox.GetValue()
self.passwordBox.SetValue("")
self.retList =
self.testBool = False
dbThread = threading.Thread(group= None, target=self.checkUser, args=(id, password, self.retList))
dbThread.start()
#TODO Blocking
print (self.retList)
#Database Thread Method
def checkUser(self, id, password, retList):
self.retList = DB.checkUser(id,password)
DB.checkUser is the method that checks the database if it has the user, and returns a list with user information, or an empty list if it does not have the user on the database.
python multithreading blocking
python multithreading blocking
edited Jan 3 at 5:44
Vikrant
3,952143756
3,952143756
asked Jan 2 at 0:54
RoBaMeRoBaMe
61
61
2
What code are you talking about when you say, "the code that comes after it."?
– Solomon Slow
Jan 2 at 1:05
2
When you talk about a mutex, don't talk about what code it protects. Code is read-only. Code does not need protection. You should be talking about what data are protected by the mutex instead.
– Solomon Slow
Jan 2 at 1:08
You want to blockprint (self.retList)
? Put it inside the thread and just return afterdbThread.start()
.
– zvone
Jan 2 at 1:18
But I need the rest of the code to not run until it finishes the action. I need all code to be blocked (the print(self.retlist) is just an example for the code that needs to be blocked, I need the code outside of the method to run only after an answer from the database was recieved) but I do need the gui thread to run so the gui would not freeze. Is there a way?
– RoBaMe
Jan 2 at 9:12
It's beginning to sound as if what you really want is for some GUI widgets to be disabled or inaccessible until the background task is finished. Why not haveonLoginButtonClick(...)
put the GUI into a mostly disabled state, havecheckUser(...)
post an event when it is finished, and have the event handler reenable all of the commands that previously were disabled?
– Solomon Slow
Jan 2 at 11:01
|
show 1 more comment
2
What code are you talking about when you say, "the code that comes after it."?
– Solomon Slow
Jan 2 at 1:05
2
When you talk about a mutex, don't talk about what code it protects. Code is read-only. Code does not need protection. You should be talking about what data are protected by the mutex instead.
– Solomon Slow
Jan 2 at 1:08
You want to blockprint (self.retList)
? Put it inside the thread and just return afterdbThread.start()
.
– zvone
Jan 2 at 1:18
But I need the rest of the code to not run until it finishes the action. I need all code to be blocked (the print(self.retlist) is just an example for the code that needs to be blocked, I need the code outside of the method to run only after an answer from the database was recieved) but I do need the gui thread to run so the gui would not freeze. Is there a way?
– RoBaMe
Jan 2 at 9:12
It's beginning to sound as if what you really want is for some GUI widgets to be disabled or inaccessible until the background task is finished. Why not haveonLoginButtonClick(...)
put the GUI into a mostly disabled state, havecheckUser(...)
post an event when it is finished, and have the event handler reenable all of the commands that previously were disabled?
– Solomon Slow
Jan 2 at 11:01
2
2
What code are you talking about when you say, "the code that comes after it."?
– Solomon Slow
Jan 2 at 1:05
What code are you talking about when you say, "the code that comes after it."?
– Solomon Slow
Jan 2 at 1:05
2
2
When you talk about a mutex, don't talk about what code it protects. Code is read-only. Code does not need protection. You should be talking about what data are protected by the mutex instead.
– Solomon Slow
Jan 2 at 1:08
When you talk about a mutex, don't talk about what code it protects. Code is read-only. Code does not need protection. You should be talking about what data are protected by the mutex instead.
– Solomon Slow
Jan 2 at 1:08
You want to block
print (self.retList)
? Put it inside the thread and just return after dbThread.start()
.– zvone
Jan 2 at 1:18
You want to block
print (self.retList)
? Put it inside the thread and just return after dbThread.start()
.– zvone
Jan 2 at 1:18
But I need the rest of the code to not run until it finishes the action. I need all code to be blocked (the print(self.retlist) is just an example for the code that needs to be blocked, I need the code outside of the method to run only after an answer from the database was recieved) but I do need the gui thread to run so the gui would not freeze. Is there a way?
– RoBaMe
Jan 2 at 9:12
But I need the rest of the code to not run until it finishes the action. I need all code to be blocked (the print(self.retlist) is just an example for the code that needs to be blocked, I need the code outside of the method to run only after an answer from the database was recieved) but I do need the gui thread to run so the gui would not freeze. Is there a way?
– RoBaMe
Jan 2 at 9:12
It's beginning to sound as if what you really want is for some GUI widgets to be disabled or inaccessible until the background task is finished. Why not have
onLoginButtonClick(...)
put the GUI into a mostly disabled state, have checkUser(...)
post an event when it is finished, and have the event handler reenable all of the commands that previously were disabled?– Solomon Slow
Jan 2 at 11:01
It's beginning to sound as if what you really want is for some GUI widgets to be disabled or inaccessible until the background task is finished. Why not have
onLoginButtonClick(...)
put the GUI into a mostly disabled state, have checkUser(...)
post an event when it is finished, and have the event handler reenable all of the commands that previously were disabled?– Solomon Slow
Jan 2 at 11:01
|
show 1 more comment
1 Answer
1
active
oldest
votes
Got the solution - delayed reaction class fro Wxpython.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000142%2fneed-thread-to-block-next-code-line-but-not-other-threads%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Got the solution - delayed reaction class fro Wxpython.
add a comment |
Got the solution - delayed reaction class fro Wxpython.
add a comment |
Got the solution - delayed reaction class fro Wxpython.
Got the solution - delayed reaction class fro Wxpython.
answered Jan 29 at 10:08
RoBaMeRoBaMe
61
61
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000142%2fneed-thread-to-block-next-code-line-but-not-other-threads%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
What code are you talking about when you say, "the code that comes after it."?
– Solomon Slow
Jan 2 at 1:05
2
When you talk about a mutex, don't talk about what code it protects. Code is read-only. Code does not need protection. You should be talking about what data are protected by the mutex instead.
– Solomon Slow
Jan 2 at 1:08
You want to block
print (self.retList)
? Put it inside the thread and just return afterdbThread.start()
.– zvone
Jan 2 at 1:18
But I need the rest of the code to not run until it finishes the action. I need all code to be blocked (the print(self.retlist) is just an example for the code that needs to be blocked, I need the code outside of the method to run only after an answer from the database was recieved) but I do need the gui thread to run so the gui would not freeze. Is there a way?
– RoBaMe
Jan 2 at 9:12
It's beginning to sound as if what you really want is for some GUI widgets to be disabled or inaccessible until the background task is finished. Why not have
onLoginButtonClick(...)
put the GUI into a mostly disabled state, havecheckUser(...)
post an event when it is finished, and have the event handler reenable all of the commands that previously were disabled?– Solomon Slow
Jan 2 at 11:01