我正在尝试编写一个线程安全的生成器。生成器代码如下所示:
def jobs(users):
result = [1 for _ in users]
while(sum(result)>0):
for n,usr in enumerate(users):
if usr.hasQuery():
yield usr
else:
result[n] = 0
我尝试了以下方法(source):
class ThreadsafeIter(object):
def __init__(self, it):
self.lock = threading.Lock()
self.it = it.__iter__()
def __iter__(self): return self
def __next__(self):
with self.lock:
return next(self.it)
def threadsafe_gen(f):
def g(*a, **kw):
return ThreadsafeIter(f(*a, **kw))
return g
然后使用@threadsafe_gen修饰了jobs()。这个线程安全的生成器使用如下:
genjobs = jobs(users)
from multiprocessing.pool import ThreadPool
pool = ThreadPool(processes=2)
pool.map(worker, genjobs)
将worker定义为
def worker(usr):
usr.runNextQuery()
实例usr具有与数据库和查询列表的pyODBC连接。调用runNextQuery时,它应该运行下一个查询。使用上面的代码,所有连接都保持空闲状态,并且不运行任何查询。知道为什么吗?