凍結我有如下使用nose_parameterized一個Python代碼:多線程的urllib2鼻子框架
from nose_parameterized import parameterized
from multiprocessing.pool import ThreadPool
import urllib2
def make_http_call(url, req_type):
opener = urllib2.build_opener() # <=== this line causes it to freeze
return 1
pool = ThreadPool(processes=4)
results = []
urls = ['a', 'b', 'c', 'd']
for url in urls:
results.append(pool.apply_async(make_http_call, (url, 'html')))
d = {'add': []}
for ind, res in enumerate(results):
d['add'].append((res.get(), 2+ind, 3+ind))
@parameterized(d['add'])
def test_add(a, b, c):
assert a+b == c
這是代碼的虛擬版本。基本上,我需要使用http請求響應加載測試參數,並且由於有很多網址,我想多線程化它們。 只要我添加urllib2.build_opener,它使用鼻子凍結(但仍然可以與python很好地工作) 另外,我試過urllib2.urlopen;一樣的問題。 是否有'正確'(可調試)解決方法的任何想法?
也許這會[幫助](http://stackoverflow.com/questions/2137187/python-process-blocked-by-urllib2) – 2014-12-05 20:48:32
謝謝。我知道你關聯的那個問題,但是我不能將任何東西放在__main__中,因爲在參數收集之後執行安裝程序,所以不能在鼻子安裝中進行設置。鑑於限制,我希望有一個解決方法。 – max 2014-12-05 21:14:02