-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththreaded.py
More file actions
83 lines (71 loc) · 2.59 KB
/
Copy paththreaded.py
File metadata and controls
83 lines (71 loc) · 2.59 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#=============================================================================
# threaded, a Python decorator for easy threading
# Copyright (C) 2012 Jure Ziberna
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#=============================================================================
import threading
import time
def threaded(function, daemon):
"""
Turns given function into a threaded function.
"""
name = '%s-%d' % (function.__name__, threading.active_count())
def function_wrapper(*args, **kwargs):
if '_delay' in kwargs:
func = delayer(function, kwargs['_delay'])
del kwargs['_delay']
else:
func = function
thread = threading.Thread(target=func, name=name, args=args, kwargs=kwargs)
thread.daemon = daemon
thread.start()
function_wrapper.__name__ = function.__name__
return function_wrapper
def delayer(function, seconds):
"""
Makes the actual call to the function delayed by given float number of
seconds. Returns a wrapped function.
"""
def delay_wrapper(*args, **kwargs):
time.sleep(seconds)
function(*args, **kwargs)
delay_wrapper.__name__ = function.__name__
return delay_wrapper
def inherit(function):
"""
Turns a function into a threaded function.
Inherits daemon property from the current thread.
"""
daemon = threading.current_thread().daemon
return threaded(function, daemon)
def daemon(function):
"""
Turns a function into a threaded function.
The thread is a daemon thread.
"""
return threaded(function, True)
def nondaemon(function):
"""
Turns a function into a threaded function.
The thread is a non-daemon thread.
"""
return threaded(function, False)
def opposite(function):
"""
Turns a function into a threaded function.
The thread's daemon property is opposite than that of the current thread.
"""
daemon = not threading.current_thread().daemon
return threaded(function, daemon)