-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmultithreaded.py
94 lines (79 loc) · 2.38 KB
/
multithreaded.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
from threading import Thread
import subprocess, os, sys
import time
import datetime
from Queue import Queue
num_threads = 75
queue = Queue()
rsync = "\"C:\\Program Files\\DeltaCopy\\rsync.exe\" -v -rlt -z --chmod=a=rw,Da+x --delete"
source = "/path/to/source"
destination = "/path/to/destination"
now = datetime.datetime.now()
logfiledate = now.strftime("%Y-%m-%d %H-%M-%S")
logfile = "D:\\logs\\threads_log" + logfiledate + "_log.txt"
def listdirs(folder):
return [d for d in os.listdir(folder) if os.path.isdir(os.path.join(folder, d))]
directories = listdirs(source)
exclude = []
print "Beginning"
print datetime.datetime.now()
file = open(logfile, 'w')
file.write("Beginning\n")
file.write(str(datetime.datetime.now()) + "\n\n\n")
file.close()
print "pausing in case of debugging"
time.sleep(30)
def syncDirectory(i, queue):
"""syncsDirectory"""
#print "running thread", i
while queue.empty != True:
dir = queue.get()
if dir in exclude:
print "excluding", dir
queue.task_done()
else:
file = open(logfile, 'a')
file.write(" Beginning"+ dir +"\n")
file.write(" " + str(datetime.datetime.now()) + "\n")
file.close()
print "syncing", dir
#print " " + source + "\\" + dir
#print " " + destination + "\\" + dir
src = source + "/" + dir + "/"
dest = destination + "/" + dir + "/"
command = rsync + " " + src + " " + dest
print command
now = datetime.datetime.now()
logfiledate = now.strftime("%Y-%m-%d %H-%M-%S")
ret = subprocess.call(command,
shell=True,
stdout=open('D:\\'+ dir + logfiledate + '-log.txt', 'a'),
stderr=subprocess.STDOUT)
#print "running dir on", dir
queue.task_done()
file = open(logfile, 'a')
file.write(" Finishing"+ dir +"\n")
file.write(" " + str(datetime.datetime.now()) + "\n")
file.close()
print "done syncing", dir
time.sleep(1)
queuesize = queue.qsize()
file = open(logfile, 'a')
file.write(" " + str(queuesize) + " remaining \n")
file.close()
print queuesize, "remaining"
for i in range(num_threads):
worker = Thread(target=syncDirectory, args=(i, queue))
worker.setDaemon(True)
worker.start()
for dir in directories:
queue.put(dir)
print "Main Thread Waiting"
queue.join()
print "Done"
print datetime.datetime.now()
file = open(logfile, 'a')
file.write("Done\n")
file.write(str(datetime.datetime.now()) + "\n")
file.close()