Skip to content

Instantly share code, notes, and snippets.

@mamaj
Last active August 25, 2022 15:46
Show Gist options
  • Save mamaj/00e6b3070f05e0feebd02b233edef4d8 to your computer and use it in GitHub Desktop.
Save mamaj/00e6b3070f05e0feebd02b233edef4d8 to your computer and use it in GitHub Desktop.
# python concurrency
import threading
import concurrent.futures
import time
import numpy as np
from operator import itemgetter
def func(worker_id, sleep):
start = time.perf_counter()
print(f'func {worker_id} started @ {threading.get_native_id()}')
time.sleep(sleep)
print(f'func {worker_id} done seleeping for {sleep} @ {threading.get_native_id()}')
duration = time.perf_counter() - start
return (worker_id, sleep, start, duration)
def main():
runtimes = np.random.randint(low=1, high=10, size=100)
start = time.perf_counter()
with concurrent.futures.ThreadPoolExecutor(max_workers=len(runtimes)) as executor:
results = []
futures = [executor.submit(func, i, s) for i, s in enumerate(runtimes)]
for f in concurrent.futures.as_completed(futures): # completion order
results.append(f.result())
# print([f.result() for f in futures]) # not in completion order
duration = time.perf_counter() - start
print(f'total duration: {duration}')
start_range = max(results, key=itemgetter(2))[2] - min(results, key=itemgetter(2))[2]
print(f'stat time error: {start_range}')
if __name__ == '__main__':
main()
print('done')
import threading
import time
def func(i=1):
print(f'func {i} started @ {threading.get_native_id()}')
time.sleep(2)
print(f'func {i} done @ {threading.get_native_id()}')
return i
# This is the main thread!
print(threading.get_native_id())
t1 = threading.Thread(target=func, args=(1,))
t1.start() # we have a new thread
t2 = threading.Thread(target=func, args=(2,))
t2.start() # we have a new thread
# at this point, both threads are started, but the main
# thread can reach here.
print(threading.active_count()) # prints 3
t1.join() # this stops the main thread at this line, untile t1 finishes.
t2.join() # wait here untile t2 finishes.
print(threading.active_count()) # prints 1
print('done')
import threading
import time
from queue import Queue
def func(sleep, queue):
start = time.perf_counter()
print(f'func {sleep} started @ {threading.get_native_id()}')
time.sleep(sleep)
print(f'func {sleep} done @ {threading.get_native_id()}')
duration = time.perf_counter() - start
queue.put((sleep, duration))
q = Queue()
# This is the main thread!
print(threading.get_native_id())
t1 = threading.Thread(target=func, args=(3, q))
t1.start() # we have a new thread
t2 = threading.Thread(target=func, args=(6, q))
t2.start() # we have a new thread
# at this point, both threads are started, but the main
# thread can reach here.
print(q.qsize())
t1.join() # this stops the main thread at this line, untile t1 finishes.
t2.join() # wait here untile t2 finishes.
print(q.qsize())
print(list(q.queue))
print('done')
import threading
import concurrent.futures
import time
def func(i=1):
print(f'func {i} started @ {threading.get_native_id()}')
time.sleep(2)
print(f'func {i} done @ {threading.get_native_id()}')
return i
# This is the main thread!
print(threading.get_native_id())
with concurrent.futures.ThreadPoolExecutor() as executor:
executor.submit(func, 1)
executor.submit(func, 2)
print(threading.activeCount()) # prints 3
# at this point, both threads are started, but the main
# thread can reach here.
print(threading.activeCount()) # prints 1
print('done')
import threading
import concurrent.futures
import time
def func(sleep):
start = time.perf_counter()
print(f'func {sleep} started @ {threading.get_native_id()}')
time.sleep(sleep)
print(f'func {sleep} done @ {threading.get_native_id()}')
duration = time.perf_counter() - start
return (sleep, duration)
# This is the main thread!
print(f'main thread: {threading.get_native_id()}')
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(func, i) for i in [1, 2]]
for f in concurrent.futures.as_completed(futures): # completion order
print(f.result())
# or by accessing the results
print([f.result() for f in futures]) # not in completion order
# or using wait:
done, not_done = concurrent.futures.wait(futures, timeout=10)
# using map
with concurrent.futures.ThreadPoolExecutor() as executor:
results = executor.map(func, [2, 1])
for result in results:
print(result)
# or wait for all:
print(list(results))
print('done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment