-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathmain.py
More file actions
53 lines (34 loc) · 1023 Bytes
/
Copy pathmain.py
File metadata and controls
53 lines (34 loc) · 1023 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
#
# https://docs.python.org/3/library/threading.html
#
import threading
import time
# --- classes ---
class ExampleThread(threading.Thread):
def __init__(self, name, counter=10, sleep=0.5):
threading.Thread.__init__(self)
self.name = name
self.counter = counter
self.sleep = sleep
def run(self):
for x in range(self.counter):
print(self.name, x)
time.sleep(self.sleep)
# --- functions ---
def example_function(name, counter=10, sleep=0.5):
for x in range(counter):
print(name, x)
time.sleep(sleep)
# --- example 1 ---
# `args` have to be tuple.
# for one argument you need `args=("function:",)`
t1 = threading.Thread(target=example_function, args=("function:", 15))
t1.start()
# --- example 2 ---
t2 = ExampleThread("class:", 10, 1.0)
t2.start()
# --- example 2 ---
# start thread after 3 seconds
t3 = threading.Timer(3, example_function, args=("** timer **:", 2, 3.0))
t3.start()