add concurrenc future example

This commit is contained in:
Mia von Steinkirch 2020-03-02 19:13:42 -08:00
parent e686184be1
commit e0a6c92e75

View file

@ -0,0 +1,23 @@
import random
import logging
import concurrent.futures
WORKER_COUNT = 10
JOB_COUNT = 10
class Job:
def __init__(self, number):
self.number = number
def process_job(job):
# Wait between 0 and 0.01 seconds.
time.sleep(random.random()/100.0)
logging.info("Job number {:d}".format(job.number))
def main():
with concurrent.futures.ThreadPoolExecutor(
max_workers=WORKER_COUNT) as executor:
futures = [executor.submit(process_job, Job(i))
for i in range(JOB_COUNT)]
for future in concurrent.futures.as_completed(futures):
pass