from Queue import Queue, Empty
from threading import Thread
THREAD_POOL_SIZE = 4
PLACES = (
'Reykjavik', 'Vien', 'Zadar', 'Venice', 'Wroclaw', 'Bolognia',
'Berlin', 'Slubice', 'New York', 'Dehli',
)
def fetch_place(place):
# geocoded = api.geocode(place)[0]
print(place)
def worker(work_queue):
while not work_queue.empty():
try:
item = work_queue.get(block=False)
except Empty:
break
else:
fetch_place(item)
work_queue.task_done()
def main():
work_queue = Queue()
for place in PLACES:
work_queue.put(place)
threads = [Thread(target=worker, args=(work_queue,))
for _ in range(THREAD_POOL_SIZE)
]
for thread in threads:
thread.start()
work_queue.join()
while threads:
threads.pop().join()
if __name__ == '__main__':
main()
python 线程池 多线程
最新推荐文章于 2024-03-12 10:06:48 发布