Python threading not stopping although daemon set to True
I assume this is an obvious one, but I am making a script which uses threading to tests proxies, and should stop once it finds a certain number of proxies. When I run it, threads stop generating output if the condition is met, but the program does not close. I've looked at other similar issues but can't seem to implement any successfully. Would appreciate any pointers.
import queue
import threading
import time
import urllib.request
class ThreadUrl(threading.Thread):
def __init__(self, queue, working_proxies):
threading.Thread.__init__(self)
self.queue = queue
self.working_proxies = working_proxies
def run(self):
while len(self.working_proxies)<5:
proxy = self.queue.get()
try:
proxy_handler = urllib.request.ProxyHandler({'http': proxy})
opener = urllib.request.build_opener(proxy_handler)
opener.addheaders = [('User-agent',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36')]
urllib.request.install_opener(opener)
req = urllib.request.Request('http://www.wikipedia.org')
sock=urllib.request.urlopen(req)
print(f'{proxy} works')
with appending_lock:
self.working_proxies.append(proxy)
except urllib.request.HTTPError as e:
print('Error code: ', e.code)
except Exception as detail:
print("ERROR:", detail)
self.queue.task_done()
def main(proxies, working_proxies):
for i in range(5):
t = ThreadUrl(queue,working_proxies)
t.daemon = True
t.start()
for proxy in proxies:
queue.put(proxy)
queue.join()
if __name__ == '__main__':
start = time.time()
appending_lock = threading.Lock()
proxies = [...list of proxies...]
working_proxies =
queue = queue.Queue()
main(proxies, working_proxies)
print("Elapsed Time: %s" % (time.time() - start))
Am I using the daemon attribute wrong, or are there other parameters I should set up in order to make sure threading stops?
python multithreading proxy urllib
add a comment |
I assume this is an obvious one, but I am making a script which uses threading to tests proxies, and should stop once it finds a certain number of proxies. When I run it, threads stop generating output if the condition is met, but the program does not close. I've looked at other similar issues but can't seem to implement any successfully. Would appreciate any pointers.
import queue
import threading
import time
import urllib.request
class ThreadUrl(threading.Thread):
def __init__(self, queue, working_proxies):
threading.Thread.__init__(self)
self.queue = queue
self.working_proxies = working_proxies
def run(self):
while len(self.working_proxies)<5:
proxy = self.queue.get()
try:
proxy_handler = urllib.request.ProxyHandler({'http': proxy})
opener = urllib.request.build_opener(proxy_handler)
opener.addheaders = [('User-agent',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36')]
urllib.request.install_opener(opener)
req = urllib.request.Request('http://www.wikipedia.org')
sock=urllib.request.urlopen(req)
print(f'{proxy} works')
with appending_lock:
self.working_proxies.append(proxy)
except urllib.request.HTTPError as e:
print('Error code: ', e.code)
except Exception as detail:
print("ERROR:", detail)
self.queue.task_done()
def main(proxies, working_proxies):
for i in range(5):
t = ThreadUrl(queue,working_proxies)
t.daemon = True
t.start()
for proxy in proxies:
queue.put(proxy)
queue.join()
if __name__ == '__main__':
start = time.time()
appending_lock = threading.Lock()
proxies = [...list of proxies...]
working_proxies =
queue = queue.Queue()
main(proxies, working_proxies)
print("Elapsed Time: %s" % (time.time() - start))
Am I using the daemon attribute wrong, or are there other parameters I should set up in order to make sure threading stops?
python multithreading proxy urllib
Voted to close, because the actual problem was a logical mistake, and not a misunderstanding about daemon threads.
– Solomon Slow
Nov 26 '18 at 15:18
add a comment |
I assume this is an obvious one, but I am making a script which uses threading to tests proxies, and should stop once it finds a certain number of proxies. When I run it, threads stop generating output if the condition is met, but the program does not close. I've looked at other similar issues but can't seem to implement any successfully. Would appreciate any pointers.
import queue
import threading
import time
import urllib.request
class ThreadUrl(threading.Thread):
def __init__(self, queue, working_proxies):
threading.Thread.__init__(self)
self.queue = queue
self.working_proxies = working_proxies
def run(self):
while len(self.working_proxies)<5:
proxy = self.queue.get()
try:
proxy_handler = urllib.request.ProxyHandler({'http': proxy})
opener = urllib.request.build_opener(proxy_handler)
opener.addheaders = [('User-agent',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36')]
urllib.request.install_opener(opener)
req = urllib.request.Request('http://www.wikipedia.org')
sock=urllib.request.urlopen(req)
print(f'{proxy} works')
with appending_lock:
self.working_proxies.append(proxy)
except urllib.request.HTTPError as e:
print('Error code: ', e.code)
except Exception as detail:
print("ERROR:", detail)
self.queue.task_done()
def main(proxies, working_proxies):
for i in range(5):
t = ThreadUrl(queue,working_proxies)
t.daemon = True
t.start()
for proxy in proxies:
queue.put(proxy)
queue.join()
if __name__ == '__main__':
start = time.time()
appending_lock = threading.Lock()
proxies = [...list of proxies...]
working_proxies =
queue = queue.Queue()
main(proxies, working_proxies)
print("Elapsed Time: %s" % (time.time() - start))
Am I using the daemon attribute wrong, or are there other parameters I should set up in order to make sure threading stops?
python multithreading proxy urllib
I assume this is an obvious one, but I am making a script which uses threading to tests proxies, and should stop once it finds a certain number of proxies. When I run it, threads stop generating output if the condition is met, but the program does not close. I've looked at other similar issues but can't seem to implement any successfully. Would appreciate any pointers.
import queue
import threading
import time
import urllib.request
class ThreadUrl(threading.Thread):
def __init__(self, queue, working_proxies):
threading.Thread.__init__(self)
self.queue = queue
self.working_proxies = working_proxies
def run(self):
while len(self.working_proxies)<5:
proxy = self.queue.get()
try:
proxy_handler = urllib.request.ProxyHandler({'http': proxy})
opener = urllib.request.build_opener(proxy_handler)
opener.addheaders = [('User-agent',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36')]
urllib.request.install_opener(opener)
req = urllib.request.Request('http://www.wikipedia.org')
sock=urllib.request.urlopen(req)
print(f'{proxy} works')
with appending_lock:
self.working_proxies.append(proxy)
except urllib.request.HTTPError as e:
print('Error code: ', e.code)
except Exception as detail:
print("ERROR:", detail)
self.queue.task_done()
def main(proxies, working_proxies):
for i in range(5):
t = ThreadUrl(queue,working_proxies)
t.daemon = True
t.start()
for proxy in proxies:
queue.put(proxy)
queue.join()
if __name__ == '__main__':
start = time.time()
appending_lock = threading.Lock()
proxies = [...list of proxies...]
working_proxies =
queue = queue.Queue()
main(proxies, working_proxies)
print("Elapsed Time: %s" % (time.time() - start))
Am I using the daemon attribute wrong, or are there other parameters I should set up in order to make sure threading stops?
python multithreading proxy urllib
python multithreading proxy urllib
asked Nov 24 '18 at 16:15
T the shirtT the shirt
298
298
Voted to close, because the actual problem was a logical mistake, and not a misunderstanding about daemon threads.
– Solomon Slow
Nov 26 '18 at 15:18
add a comment |
Voted to close, because the actual problem was a logical mistake, and not a misunderstanding about daemon threads.
– Solomon Slow
Nov 26 '18 at 15:18
Voted to close, because the actual problem was a logical mistake, and not a misunderstanding about daemon threads.
– Solomon Slow
Nov 26 '18 at 15:18
Voted to close, because the actual problem was a logical mistake, and not a misunderstanding about daemon threads.
– Solomon Slow
Nov 26 '18 at 15:18
add a comment |
1 Answer
1
active
oldest
votes
After going through the docs and a few other resources, I found that the threads did close properly, but queue.join() function was blocking until the queue was emptied. Since that was not going to happen if the threads were killed before finishing all items from the queue, the script continued to run.
So, I have overridden the queue.join() to look like this:
queue = queue.Queue()
def waiter(queue):
while not queue.empty() and dead == False:
pass
queue.join = waiter
The 'dead' variable is accessed within a thread if it establishes that a closing condition is fulfilled. In the code above, 'dead' would be put instead of 'len(self.working_proxies)<5' in the while loop of the run() function. Each time a new item is appended to self.working_proxies, the script should check if the condition for terminating has been met, and, if so, set 'dead' to True.
Also, 'dead' is a global variable, so it does not need to be passed into the new waiter() function.
I'm sure there is a more elegant way to approach the issue, but this one should do the trick in the meanwhile.
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%2f53460034%2fpython-threading-not-stopping-although-daemon-set-to-true%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
After going through the docs and a few other resources, I found that the threads did close properly, but queue.join() function was blocking until the queue was emptied. Since that was not going to happen if the threads were killed before finishing all items from the queue, the script continued to run.
So, I have overridden the queue.join() to look like this:
queue = queue.Queue()
def waiter(queue):
while not queue.empty() and dead == False:
pass
queue.join = waiter
The 'dead' variable is accessed within a thread if it establishes that a closing condition is fulfilled. In the code above, 'dead' would be put instead of 'len(self.working_proxies)<5' in the while loop of the run() function. Each time a new item is appended to self.working_proxies, the script should check if the condition for terminating has been met, and, if so, set 'dead' to True.
Also, 'dead' is a global variable, so it does not need to be passed into the new waiter() function.
I'm sure there is a more elegant way to approach the issue, but this one should do the trick in the meanwhile.
add a comment |
After going through the docs and a few other resources, I found that the threads did close properly, but queue.join() function was blocking until the queue was emptied. Since that was not going to happen if the threads were killed before finishing all items from the queue, the script continued to run.
So, I have overridden the queue.join() to look like this:
queue = queue.Queue()
def waiter(queue):
while not queue.empty() and dead == False:
pass
queue.join = waiter
The 'dead' variable is accessed within a thread if it establishes that a closing condition is fulfilled. In the code above, 'dead' would be put instead of 'len(self.working_proxies)<5' in the while loop of the run() function. Each time a new item is appended to self.working_proxies, the script should check if the condition for terminating has been met, and, if so, set 'dead' to True.
Also, 'dead' is a global variable, so it does not need to be passed into the new waiter() function.
I'm sure there is a more elegant way to approach the issue, but this one should do the trick in the meanwhile.
add a comment |
After going through the docs and a few other resources, I found that the threads did close properly, but queue.join() function was blocking until the queue was emptied. Since that was not going to happen if the threads were killed before finishing all items from the queue, the script continued to run.
So, I have overridden the queue.join() to look like this:
queue = queue.Queue()
def waiter(queue):
while not queue.empty() and dead == False:
pass
queue.join = waiter
The 'dead' variable is accessed within a thread if it establishes that a closing condition is fulfilled. In the code above, 'dead' would be put instead of 'len(self.working_proxies)<5' in the while loop of the run() function. Each time a new item is appended to self.working_proxies, the script should check if the condition for terminating has been met, and, if so, set 'dead' to True.
Also, 'dead' is a global variable, so it does not need to be passed into the new waiter() function.
I'm sure there is a more elegant way to approach the issue, but this one should do the trick in the meanwhile.
After going through the docs and a few other resources, I found that the threads did close properly, but queue.join() function was blocking until the queue was emptied. Since that was not going to happen if the threads were killed before finishing all items from the queue, the script continued to run.
So, I have overridden the queue.join() to look like this:
queue = queue.Queue()
def waiter(queue):
while not queue.empty() and dead == False:
pass
queue.join = waiter
The 'dead' variable is accessed within a thread if it establishes that a closing condition is fulfilled. In the code above, 'dead' would be put instead of 'len(self.working_proxies)<5' in the while loop of the run() function. Each time a new item is appended to self.working_proxies, the script should check if the condition for terminating has been met, and, if so, set 'dead' to True.
Also, 'dead' is a global variable, so it does not need to be passed into the new waiter() function.
I'm sure there is a more elegant way to approach the issue, but this one should do the trick in the meanwhile.
answered Nov 24 '18 at 22:39
T the shirtT the shirt
298
298
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%2f53460034%2fpython-threading-not-stopping-although-daemon-set-to-true%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
Voted to close, because the actual problem was a logical mistake, and not a misunderstanding about daemon threads.
– Solomon Slow
Nov 26 '18 at 15:18