Skip to content

Instantly share code, notes, and snippets.

@cimi
Last active August 29, 2015 14:19
Show Gist options
  • Save cimi/1e2335dcda7e0b4bb637 to your computer and use it in GitHub Desktop.
Save cimi/1e2335dcda7e0b4bb637 to your computer and use it in GitHub Desktop.
python threading example
from itertools import groupby
from threading import Thread
from random import randint
from time import sleep
# see http://stackoverflow.com/questions/29291270/threading-in-python-processing-multiple-large-files-concurrently
for key, partition in groupby(range(1, 50), lambda k: k//10):
threads = []
for idx in list(partition):
thread_name = 'thread-%d' % idx
t = Thread(name=thread_name, target=sleep, args=(randint(1, 5),))
threads.append(t)
print 'Starting %s' % t.getName()
t.start()
for t in threads:
print 'Joining %s' % t.getName()
t.join()
print 'Joined the first group of %s' % map(lambda t: t.getName(), threads)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment