Skip to content

Instantly share code, notes, and snippets.

@robbintt
Created June 20, 2015 00:38
Show Gist options
  • Save robbintt/9faccf7d8f5bfc9e84ee to your computer and use it in GitHub Desktop.
Save robbintt/9faccf7d8f5bfc9e84ee to your computer and use it in GitHub Desktop.
python threading example
import threading
import Queue
import time
import fileinput
import sys
validated_stdin = Queue.Queue()
def my_threaded_func(line):
print "Running thread for line:", line
time.sleep(2)
print "Completed thread for line:", line
def get_input(stdin):
for line in iter(stdin.readline, ''):
validated_stdin.put(line)
stdin.close()
# How will this loop fit into django?
def queue_validated_line():
while True:
if not validated_stdin.empty():
validated_line = validated_stdin.get()
processor_thread = threading.Thread(target=my_threaded_func, args=(validated_line,))
processor_thread.start()
print "passing line to thread: " + validated_line
# this is a separate thread allowing stdin to go forever.
stdin_thread = threading.Thread(target=get_input, args=(sys.stdin,))
stdin_thread.start()
queue_validated_line_thread = threading.Thread(target=queue_validated_line)
queue_validated_line_thread.start()
print "This is the end of the main thread. It falls off right away."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment