-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_inpython_161225.py
More file actions
52 lines (37 loc) · 1.24 KB
/
thread_inpython_161225.py
File metadata and controls
52 lines (37 loc) · 1.24 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
Now it's time for me to start thread in Python.
I have to start from scratch so let's start step by step.
Start date = 2016/12/25
End date = 201?/??/??
"""
import time
import threading
import datetime
# In python, threading module is almost what we're looking for.
# It means there are also other options but not recommended.
##################### Step 1. ###################
################## very basic ###################
#################################################
class Messenger(threading.Thread):
def run(self):
for _ in range(10):
print(threading.currentThread().getName())
time.sleep(0.1)
send = Messenger(name='Sending out messages')
receive = Messenger(name='Receiving out messages')
send.start()
receive.start()
"""
In Pyhton, you have to override run method to get your thread started.
start method executes run method.
"""
##################### Step 2. ###################
################## Another example ##############
#################################################
class TimeThread(threading.Thread):
def run(self):
now = datetime.datetime.now()
print('Hello world! Its {} and {} now!'.format(self.getName(), now))
for _ in range(3):
t = TimeThread()
t.start()