Last active
August 29, 2015 14:19
-
-
Save cimi/1e2335dcda7e0b4bb637 to your computer and use it in GitHub Desktop.
python threading example
This file contains hidden or 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 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