Created
August 28, 2018 23:25
-
-
Save flockonus/d88ce74a8f7a4b164b3fa83e3ade31df to your computer and use it in GitHub Desktop.
threading experiments in python 3.7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
long running python tread with ThreadPoolExecutor, satisfying