Skip to content

Instantly share code, notes, and snippets.

@kounoike
Created August 30, 2014 16:41
Show Gist options
  • Save kounoike/adc9d2e211423aae8fdb to your computer and use it in GitHub Desktop.
Save kounoike/adc9d2e211423aae8fdb to your computer and use it in GitHub Desktop.
# 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