我是python中gevent
的新手。我可以实现如下的多线程逻辑:
results = []
lock = threading.Lock()
threads = []
def fetch(page_num):
data = get_some_data_from_url(page_num)
lock.acquire()
results.append(data)
lock.release()
for i in range(1, 10):
thread = threading.Thread(target=fetch, args=(i,))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
如何使用gevent
实现此目的?我知道gevent.spawn()
函数,但我无法在代码中执行独占访问共享变量,如results
。我可以吗?