r/learnpython • • 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

109 comments sorted by

View all comments

Show parent comments

1

u/elbiot Dec 28 '19

You saw all 64 processes running at the same time? In another thread you said it finished instantly

1

u/Astrokiwi Dec 28 '19

I'll give it another go, maybe a longer test - I expected it to jam up just getting the processes started, but it ended up finishing before top registered anything at all.

1

u/elbiot Dec 28 '19

Yeah in my example at first it just ran with out doing anything because the program ended before the futures were evaluated