Skip to content

Instantly share code, notes, and snippets.

@tonyallan
Last active May 12, 2021 01:18
Show Gist options
  • Save tonyallan/26016e54eec2b0bc6613c7c398040c95 to your computer and use it in GitHub Desktop.
Save tonyallan/26016e54eec2b0bc6613c7c398040c95 to your computer and use it in GitHub Desktop.
A Python 3 example of multiple threads including user input
#!python3
import threading
import queue
import time
import fileinput
import sys
def worker_thread1():
while True:
print(threading.get_ident(), 'worker_thread1 Hello')
time.sleep(2)
def worker_thread2(input_queue):
while True:
text = input_queue.get()
print(threading.get_ident(), 'worker_thread2 More input to process', text)
def input_thread(input_queue):
while True:
print(threading.get_ident(), 'input_thread > ')
text = input()
input_queue.put(text)
def main():
input_queue = queue.Queue()
wt1 = threading.Thread(target=worker_thread1)
wt1.start()
wt2 = threading.Thread(target=worker_thread2, args=(input_queue,))
wt2.start()
it = threading.Thread(target=input_thread, args=(input_queue,))
it.start()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment