I have a problem with urlopen (and request.get)
In my program, if I run it inside a thread (I tested using it multiprocessingtoo) [update: thread created by the imported module], it will not work until the program finishes.
By default, it doesn’t start, I mean it doesn’t even start: the timeout (here 3 seconds) will never work, and no will be created on the website .
Here is my simplified code:
import threading,urllib2,time
def dlfile(url):
print 'Before request'
r = urllib2.urlopen(url, timeout=3)
print 'After request'
return r
def dlfiles(*urls):
threads = [threading.Thread(None, dlfile, None, (url,), {}) for url in urls]
map(lambda t:t.start(), threads)
def main():
dlfiles('http://google.com')
main()
time.sleep(10)
print 'End of program'
My conclusion:
Before request
End of program
After request
Unfortunately, the code that I write in SO works as expected (ie, "Before requesting / after requesting / completing the program"), and I still cannot reproduce the problem with simplified code.
, , - - . , , .
, , , interwebs
UPDATE
threadtest.py
import threading,urllib2,time
def log(a):print(a)
def dlfile(url):
log('Before request')
r = urllib2.urlopen(url, timeout=3)
log('After request')
return r
def dlfiles(*urls):
threads = [threading.Thread(None, dlfile, None, (url,), {}) for url in urls]
map(lambda t:t.start(), threads)
def main():
dlfiles('http://google.com')
main()
for i in range(5):
time.sleep(1)
log('Sleep')
log('End of program')
threadtest-import.py
import threadtest
:
$ python threadtest.py
Before request
After request
Sleep
Sleep
Sleep
Sleep
Sleep
End of program
$ python threadtest-import.py
Before request
Sleep
Sleep
Sleep
Sleep
Sleep
End of program
After request
, , : ? ?
? , urlopen, .