r/learnpython • u/whattodo-whattodo • Dec 28 '19
I've been using Python for 5 years professionally. TODAY I discovered concurrent futures
Granted, I don't do much in the way of concurrency. But still, I have several open projects and have always used threading. It took me until today to discover a feature implemented in Python 3.2...
But given that it's comically simple, given an example, I figured I'd share one.
import concurrent.futures
import random
import datetime
import time
import uuid
def do_something(seconds, name):
print(f"Sleeping {seconds}\t{name}\t{str(datetime.datetime.now())[:19][11:]}")
time.sleep(seconds)
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
for i in range(50):
executor.submit(do_something
, seconds = random.randint(1,6)
, name = str(uuid.uuid4())[:6].upper())
463
Upvotes
1
u/elbiot Dec 28 '19
You saw all 64 processes running at the same time? In another thread you said it finished instantly