Python threads with urllib
I use python to request a web service with many requests in the same time. To do so I create threads and use urllib 开发者_如何学Go(first version, I use python 2.6). When I start the threads, all goes well until one reach the ulllib.urlopen(). The second thread has to wait until the first one end before passing through the ulllib.urlopen() function. As I do a lot of work after having retrieved the Json from remote web service, I wish the second thread to "urlopen" in the same time or just after the first one closes its socket.
I tried closing the socket opened just after having collected the JSON returned but it changes nothing. The second thread has to wait for the first one to be ended. To see that I use prints.
I can understand that urllib isn't thread-safe (google this doesn't give clear answers) but why does the second thread has to wait for the first-one end (and not just the socket process end) ?
Thanks for your help and hints
PS: I do not use Python 3 for compatibility with modules / packages I require
This does not sounds intended behavior as two parallel urllib request should be possible. Are you sure your remote server can handle two paraller requests (e.g. it is not in debug mode with a single thread)?
Any case: threading is not a preferred approach for parallel programming with Python. Either use processes or async, especially on the server side (you didn't mention the use case or your platform which may also be buggy).
I have had very good experiences processing and transforming JSON/XML with Spawning and Eventlets which patch Python socket code to be asynchronous.
http://pypi.python.org/pypi/Spawning/
http://eventlet.net/
精彩评论