I'm trying to run multiple threads with urllib2 w/ cookies. I have a function like the one below which is run in about 5 threads simultaneously. I'm not installing the opener just running as is in each thread.
def myfunction(inputvar):
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
Is this thread safe? if it isn't is it because python modules themselves need to be thread safe re开发者_高级运维gardless of scope?
That should be thread-safe. If you look at the source code for urllib2.py neither the _build_opener_ function nor the HTTPCookieProcessor() use any global state. The _build_opener_ function returns a new OpenerDirector object which is also self-contained. So each thread will have its own set of objects that won't interfere with one another.
精彩评论