mirror of
https://github.com/autistic-symposium/web3-starter-py.git
synced 2025-05-17 14:10:21 -04:00
add concurrenc future example
This commit is contained in:
parent
e686184be1
commit
e0a6c92e75
1 changed files with 23 additions and 0 deletions
23
Concurrence_examples/concurrence_future_example.py
Normal file
23
Concurrence_examples/concurrence_future_example.py
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue