Skip to content

Instantly share code, notes, and snippets.

@flockonus
Created August 28, 2018 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flockonus/d88ce74a8f7a4b164b3fa83e3ade31df to your computer and use it in GitHub Desktop.
Save flockonus/d88ce74a8f7a4b164b3fa83e3ade31df to your computer and use it in GitHub Desktop.
threading experiments in python 3.7
from threading import Thread
from queue import Queue
import time
num_worker_threads = 1
print("start", time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))
def task(item):
print("IN", item)
time.sleep(item)
print("OUT", item)
def worker():
while True:
item = q.get()
task(item)
q.task_done()
q = Queue()
for i in range(num_worker_threads):
t = Thread(target=worker)
t.daemon = True
t.start()
for item in range(4):
q.put(item)
q.join()
print("done", time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))
import time
from concurrent import futures
def task(item):
print("IN", item)
time.sleep(item)
print("OUT", item)
exec = futures.ThreadPoolExecutor(max_workers=2)
for item in range(4):
exec.submit(task, item)
print("checkpoint 1")
for item in range(4):
exec.submit(task, item)
print("checkpoint 2")
@flockonus
Copy link
Author

long running python tread with ThreadPoolExecutor, satisfying

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment