Created
August 30, 2014 16:41
-
-
Save kounoike/adc9d2e211423aae8fdb to your computer and use it in GitHub Desktop.
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
# coding: utf-8 | |
import os | |
import sys | |
import threading | |
import Queue | |
import logging | |
import time | |
num_workers = 3 | |
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(threadName)s %(message)s") | |
q = Queue.Queue() | |
class WorkerThread(threading.Thread): | |
def run(self): | |
while True: | |
item = q.get() | |
logging.info("get item: %d", item) | |
if item == 4: | |
q.put(10) | |
t = WorkerThread(name="new thread") | |
t.daemon = True | |
t.start() | |
q.task_done() | |
break | |
q.task_done() | |
for i in range(num_workers): | |
t = WorkerThread(name="t{0:d}".format(i)) | |
t.daemon = True | |
t.start() | |
for item in [1, 4, 3, 5, 2, 7, 8, 6, 9]: | |
time.sleep(item) | |
q.put(item) | |
q.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment