- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlesson05_queues.py
More file actions
Latest commit
27 lines (25 loc) · 713 Bytes
/
Copy pathlesson05_queues.py
File metadata and controls
27 lines (25 loc) · 713 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# SuperFastPython.com
# example of threads sharing data using a queue
fromtimeimportsleep
fromthreadingimportThread
fromqueueimportQueue
# custom function to be executed in a new thread
deftask(shared_queue):
# block for a moment
sleep(1)
# prepare some data
item='Hello from a new thread'
# share the data via the queue
shared_queue.put(item)
# protect the entry point
if__name__=='__main__':
# create the shared queue
queue=Queue()
# create a new thread
thread=Thread(target=task, args=(queue,))
# start the thread
thread.start()
# block and wait for data via queue
data=queue.get()
# report data from the queue
print(data)