开发者

is this python code with urllib2 with cookies thread safe?

开发者 https://www.devze.com 2023-01-28 12:10 出处:网络
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 e

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号