mirror of
https://github.com/autistic-symposium/web3-starter-py.git
synced 2025-05-18 06:30:23 -04:00
add concurrence examples
This commit is contained in:
parent
614452d462
commit
516c922e02
15 changed files with 64 additions and 248 deletions
|
@ -1,36 +1,19 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import time
|
||||
from queue import Queue
|
||||
from threading import Thread
|
||||
|
||||
|
||||
|
||||
NUM_WORKERS = 4
|
||||
task_queue = Queue()
|
||||
|
||||
|
||||
|
||||
def worker():
|
||||
# Constantly check the queue for addresses
|
||||
while True:
|
||||
address = task_queue.get()
|
||||
run_function(address)
|
||||
|
||||
# Mark the processed task as done
|
||||
task_queue.task_done()
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
# Create the worker threads
|
||||
|
||||
|
||||
threads = [Thread(target=worker) for _ in range(NUM_WORKERS)]
|
||||
|
||||
# Add the websites to the task queue
|
||||
[task_queue.put(item) for item in SOME_LIST]
|
||||
|
||||
# Start all the workers
|
||||
[task_queue.put(item) for item in threads]
|
||||
[thread.start() for thread in threads]
|
||||
|
||||
# Wait for all the tasks in the queue to be processed
|
||||
task_queue.join()
|
||||
|
||||
|
||||
end_time = time.time()
|
||||
|
||||
print('Time: {} secs'.format(end_time - start_time))
|
||||
task_queue.join()
|
Loading…
Add table
Add a link
Reference in a new issue