-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
77 lines (55 loc) · 1.8 KB
/
utils.py
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
#coding: utf-8
import threading
import functools
import os
class myThread(threading.Thread):
__threadCount = 0
# __lock = threading.Lock()
def __init__(self, name, function, queue, lock):
#输入参数:线程名称, 线程函数, 线程函数处理的队列, 线程锁
threading.Thread.__init__(self, name=name)
self._threadID = self.__threadCount
self.__threadCount += 1
# self._name = name
self._func = function
self._queue = queue
self.__lock = lock
self._exitflag = 0
def run(self):
# print("开始线程: " + self._name)
# queueLock = threading.Lock()
while not self._exitflag:
self.__lock.acquire()
# queueLock.acquire()
if not self._queue.empty():
data = self._queue.get()
self.__lock.release()
self._func(data)
else:
self.__lock.release()
# queueLock.release()
# print("退出线程: {}".format(self._name))
@property
def threadID(self):
return self._threadID
def exit(self):
self._exitflag = 1
class chdir():
def __init__(self, newdir):
self._olddir = os.getcwd()
self._newdir = newdir
def __enter__(self):
os.chdir(self._newdir)
# print("enter work dir", self._newdir)
def __exit__(self, a, b, c):
os.chdir(self._olddir)
# print("exit work dir ", self._newdir)
def log(text=None):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kw):
if text is not None:
print( '%s %s %s():' % (text, func.__module__, func.__name__))
return func(*args, **kw)
return wrapper
return decorator