- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlesson05_global_variables.py
More file actions
Latest commit
26 lines (24 loc) · 672 Bytes
/
Copy pathlesson05_global_variables.py
File metadata and controls
26 lines (24 loc) · 672 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
# SuperFastPython.com
# example of threads sharing data with global variables
fromtimeimportsleep
fromthreadingimportThread
# custom function to be executed in a new thread
deftask():
# block for a moment
sleep(1)
# correctly scope the global variable
globaldata
# store data in the global variable
data='Hello from a new thread'
# protect the entry point
if__name__=='__main__':
# define the global variable
data=None
# create a new thread
thread=Thread(target=task)
# start the thread
thread.start()
# wait for the thread to finish
thread.join()
# report the global variable
print(data)