-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransferManager.py
70 lines (54 loc) · 2.09 KB
/
TransferManager.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
62
63
64
65
66
67
68
69
70
import threading
import enum
import time
class StreamFlow(enum.Enum):
U = Upload = 0
D = Download = 1
class TransferStatus(enum.Enum):
End = 0
Ready = 1
class TransferRecord:
def __init__(self, flow, source, dest, status=TransferStatus.Ready):
self.flow = flow
self.status = status
self.source = source
self.dest = dest
def set_status(self, status):
self.status = status
def transfer_wrapper(transfer_function, flow, records, lock):
while True:
lock.acquire()
print(records)
r = None
for _r in records:
if _r.flow==flow and _r.status==TransferStatus.Ready:
r = _r
lock.release()
if r is not None:
# self.push_function(self.filename, Path(os.path.join(self.remote_dir, self.filename.split('/')[-1])).as_posix())
transfer_function(r.source, r.dest)
lock.acquire()
r.set_status(TransferStatus.End)
lock.release()
time.sleep(0.2)
class TransferManager:
def __init__(self, upload_function=None, download_function=None):
self.upload_function = upload_function
self.download_function = download_function
self.list_lock = threading.Lock() # for records
self.records = []
self.thread_list = [0, 1]
def add_records(self, *argv):
self.records.append(TransferRecord(*argv, status=TransferStatus.Ready))
def threads(self):
self.thread_list[0] = threading.Thread(
target=transfer_wrapper, args=(self.upload_function,StreamFlow.Upload,self.records,self.list_lock))
self.thread_list[1] = threading.Thread(
target=transfer_wrapper, args=(self.download_function,StreamFlow.D,self.records,self.list_lock))
self.thread_list[0].setDaemon(True)
self.thread_list[1].setDaemon(True)
self.thread_list[0].start()
self.thread_list[1].start()
def set_function(self, upload_function, download_function):
self.upload_function = upload_function
self.download_function = download_function