Skip to content

Commit e393992

Browse files
committed
The schedule module is not designed to run in a non-blocking fashion
This forces code to run sequentially on the scheduler thread. fixes #231 (again)
1 parent eef7d84 commit e393992

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

mmpy_bot/scheduler.py

+16-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from datetime import datetime
22
from multiprocessing import Pipe, Process
33
from multiprocessing.connection import Connection
4-
from threading import Thread
54
from typing import Optional
65

76
import schedule
@@ -38,26 +37,24 @@ def _run_job(self, job):
3837
event loop.
3938
"""
4039

41-
def launch_and_wait():
42-
# Launch job in a dedicated process and send the result through a pipe.
43-
if "subprocess" in job.tags:
40+
# Launch job in a dedicated process and send the result through a pipe.
41+
if "subprocess" in job.tags:
4442

45-
def wrapped_run(pipe: Connection):
46-
result = job.run()
47-
pipe.send(result)
48-
49-
pipe, child_pipe = Pipe()
50-
p = Process(target=wrapped_run, args=(child_pipe,))
51-
p.start()
52-
result = pipe.recv()
53-
else:
54-
# Or simply run the job in this thread
43+
def wrapped_run(pipe: Connection):
5544
result = job.run()
56-
57-
if isinstance(result, schedule.CancelJob) or result is schedule.CancelJob:
58-
self.cancel_job(job)
59-
60-
Thread(target=launch_and_wait).start()
45+
pipe.send(result)
46+
47+
pipe, child_pipe = Pipe()
48+
p = Process(target=wrapped_run, args=(child_pipe,))
49+
p.start()
50+
# This still blocks despite running in a subprocess
51+
result = pipe.recv()
52+
else:
53+
# Or simply run the job in this thread
54+
result = job.run()
55+
56+
if isinstance(result, schedule.CancelJob) or result is schedule.CancelJob:
57+
self.cancel_job(job)
6158

6259

6360
def _once(trigger_time: Optional[datetime] = None):

0 commit comments

Comments
 (0)