forked from gil9red/SimplePyScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.py
More file actions
45 lines (32 loc) · 884 Bytes
/
Copy paththread.py
File metadata and controls
45 lines (32 loc) · 884 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'ipetrash'
from threading import Thread
def thread(my_func):
def wrapper(*args, **kwargs):
my_thread = Thread(target=my_func, args=args, kwargs=kwargs)
my_thread.start()
return wrapper
if __name__ == '__main__':
@thread
def _print_and_sleep(timeout=2):
print('start. print_and_sleep')
import time
time.sleep(timeout)
print('finish. print_and_sleep')
@thread
def _print_loop(name, max_num=10):
print('start. _print_loop')
import time
i = 0
while True:
print(name, i)
time.sleep(1)
i += 1
if i == max_num:
break
print('finish. _print_loop')
_print_and_sleep()
_print_and_sleep(4)
_print_and_sleep()
_print_loop(' loop')