Created
August 13, 2015 16:38
-
-
Save micviklui/79b4f5cb3b71b5367f67 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
#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