From 95558fcc1d9a673688eca88000207ac9439e172d Mon Sep 17 00:00:00 2001 From: lowercase00 <21188280+lowercase00@users.noreply.github.com> Date: Thu, 2 Mar 2023 21:46:07 -0300 Subject: [PATCH] docs: scheduler safe import (#1835) * docs: scheduler safe import * docs: rollback main block --- docs/docs/scheduling.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/docs/scheduling.md b/docs/docs/scheduling.md index 03aa1fa..cb297fa 100644 --- a/docs/docs/scheduling.md +++ b/docs/docs/scheduling.md @@ -95,8 +95,8 @@ from rq import Worker, Queue from redis import Redis redis = Redis() - queue = Queue(connection=redis) + worker = Worker(queues=[queue], connection=redis) worker.work(with_scheduler=True) ``` @@ -113,5 +113,26 @@ working. This way, if a worker with active scheduler dies, the scheduling work w up by other workers with the scheduling component enabled. +## Safe importing of the worker module + +When running the worker programmatically with the scheduler, you must keep in mind that the +import must be protected with `if __name__ == '__main__'`. The scheduler runs on it's own process +(using `multiprocessing` from the stdlib), so the new spawned process must able to safely import the module without +causing any side effects (starting a new process on top of the main ones). + +```python +... + +# When running `with_scheduler=True` this is necessary +if __name__ == '__main__': + worker = Worker(queues=[queue], connection=redis) + worker.work(with_scheduler=True) + +... +# When running without the scheduler this is fine +worker = Worker(queues=[queue], connection=redis) +worker.work() +``` +More information on the Python official docs [here](https://docs.python.org/3.7/library/multiprocessing.html#the-spawn-and-forkserver-start-methods).