Skip to content

Commit 7ece67d

Browse files
authored
add mutex
1 parent c98abc6 commit 7ece67d

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Mirrors-AutoSync.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# ===========================================
1414

1515
from apscheduler.schedulers.background import BackgroundScheduler,BlockingScheduler
16+
import threading
1617
import json
1718
import datetime, time
1819
import os
@@ -45,12 +46,15 @@ def __init__(self, name, schedule, exec, argument):
4546
def runner(self):
4647
global method_lock
4748
global scheduler
49+
global mutex
4850

4951
while method_lock > 1:
5052
print(" [{}] waiting.".format(self.name))
5153
time.sleep(10)
5254

53-
method_lock += 1
55+
if mutex.acquire():
56+
method_lock += 1
57+
mutex.release()
5458

5559
try:
5660

@@ -77,7 +81,10 @@ def runner(self):
7781
self.name,
7882
next_run_time = datetime.datetime.now() + datetime.timedelta(seconds = 10*60),)
7983
finally:
80-
method_lock -= 1
84+
85+
if mutex.acquire():
86+
method_lock -= 1
87+
mutex.release()
8188

8289
def setup(self, scheduler):
8390
scheduler.add_job(
@@ -130,6 +137,7 @@ def setup(self, scheduler):
130137
status_file_dir = content['status_file_dir']
131138

132139
method_lock = 0
140+
mutex = threading.Lock()
133141

134142
for i in content['schedules']:
135143
t = task(i['name'], i['schedule'], i['exec'], i['argument'])

0 commit comments

Comments
 (0)