mirror of
https://github.com/autistic-symposium/web3-starter-py.git
synced 2025-06-12 09:02:51 -04:00
add more concurrent examples
This commit is contained in:
parent
6c65e7d226
commit
f35541a061
3 changed files with 104 additions and 0 deletions
Concurrence_examples
34
Concurrence_examples/threads_with_queues.py
Normal file
34
Concurrence_examples/threads_with_queues.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
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
|
||||
[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: %ssecs" % (end_time - start_time))
|
Loading…
Add table
Add a link
Reference in a new issue