- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlesson05_instance_variables.py
More file actions
Latest commit
32 lines (29 loc) · 830 Bytes
/
Copy pathlesson05_instance_variables.py
File metadata and controls
32 lines (29 loc) · 830 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
28
29
30
31
32
# SuperFastPython.com
# example of thread return values via instance variables
fromtimeimportsleep
fromthreadingimportThread
# custom thread
classCustomThread(Thread):
# constructor
def__init__(self):
# execute the base constructor
Thread.__init__(self)
# set a default value
self.value=None
# function executed in a new thread
defrun(self):
# block for a moment
sleep(1)
# store data in an instance variable
self.value='Hello from a new thread'
# protect the entry point
if__name__=='__main__':
# create a new thread
thread=CustomThread()
# start the thread
thread.start()
# wait for the thread to finish
thread.join()
# get the value returned from the thread
data=thread.value
print(data)