Skip to content

Instantly share code, notes, and snippets.

@micviklui
Created August 13, 2015 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micviklui/79b4f5cb3b71b5367f67 to your computer and use it in GitHub Desktop.
Save micviklui/79b4f5cb3b71b5367f67 to your computer and use it in GitHub Desktop.
#http://www.troyfawkes.com/learn-python-multithreading-queues-basics/
#http://lonelycode.com/2011/02/04/python-threading-and-queues-and-why-its-awesome/
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
import time
import logging
import threading
from Queue import Queue
LOGGER = logging.getLogger(__name__)
logging.basicConfig(
level=logging.DEBUG,
format='[%(levelname)s] (%(threadName)-10s) %(message)s',
)
def process():
# running in a thread
while True:
element = pending_actions.get()
LOGGER.debug("processing e = {}".format(element))
LOGGER.debug("queue = {}".format(pending_actions))
time.sleep(1)
pending_actions = Queue()
threading.Thread(target=process, name="processor").start()
elements = 'asdfqwer'
for e in elements:
LOGGER.debug("queueing e = {}".format(e))
pending_actions.put(e)
while True:
time.sleep(1)
if pending_actions.empty():
LOGGER.debug("empty: stop")
break
LOGGER.debug("not empty")
threads_alive = threading.enumerate()
for t in threads_alive:
#if t == threading.current_thread():
# continue
t.join()
#threads_alive = threading.enumerate()
#threads_alive.remove(threading.current_thread())
#if threads_alive:
# LOGGER.warning("These threads are still alive: {}".format(threads_alive))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment