|
1 | 1 | from datetime import datetime
|
2 | 2 | from multiprocessing import Pipe, Process
|
3 | 3 | from multiprocessing.connection import Connection
|
4 |
| -from threading import Thread |
5 | 4 | from typing import Optional
|
6 | 5 |
|
7 | 6 | import schedule
|
@@ -38,26 +37,24 @@ def _run_job(self, job):
|
38 | 37 | event loop.
|
39 | 38 | """
|
40 | 39 |
|
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: |
44 | 42 |
|
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): |
55 | 44 | 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) |
61 | 58 |
|
62 | 59 |
|
63 | 60 | def _once(trigger_time: Optional[datetime] = None):
|
|
0 commit comments