Comments (15)
This is how I'm currently autoreloading the worker process. This is a management command I called devrqworker, so instead of running manage.py rqworker
I simply run manage.py devrqworker
during dev.
import os
import shlex
import subprocess
from django.core.management.base import BaseCommand
from django.utils.autoreload import run_with_reloader
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('queues', nargs='+', type=str)
parser.add_argument('--worker-pid-file', action='store', dest='worker_pid_file', default='/tmp/rqworker.pid')
def handle(self, *args, **options):
run_with_reloader(run_worker, options['queues'], options['worker_pid_file'])
def run_worker(queues, worker_pid_file):
if os.path.exists(worker_pid_file):
worker_pid = subprocess.run(['cat', worker_pid_file], stdout=subprocess.PIPE).stdout.decode('utf-8')
kill_worker_cmd = f'kill {worker_pid}'
subprocess.run(shlex.split(kill_worker_cmd), stderr=subprocess.PIPE)
queues = ' '.join(queues)
start_worker_cmd = f'{get_managepy_path()} rqworker {queues} --pid={worker_pid_file}'
subprocess.run(shlex.split(start_worker_cmd))
def get_managepy_path():
managepy_path = None
search_path = os.path.dirname(__file__)
while not managepy_path:
if os.path.exists(os.path.join(search_path, 'manage.py')):
managepy_path = os.path.abspath(os.path.join(search_path, 'manage.py'))
else:
search_path = os.path.join(search_path, '../')
return managepy_path
from rq.
One solution for this would be to write a script that runs workers in --burst
mode infinitely.
Example:
#!/usr/bin/env bash
while true; do
rqworker --burst
sleep 1
done
Of course it won't work reliably if you have long monolithic jobs, but in that case you have a bigger problem than reloading your workers ;)
from rq.
Was going through the code and I couldn't really see why you'd have to reload. Aren't functions "imported" using getattr during execution:
Lines 191 to 200 in 549648b
I tested this real-quick by starting a worker where the job initially made a file "old" and issued a job (the file "old" was made). Then I deleted that file, changed the filename to "new", and issued another job (without restarting the worker). A "new" file was made. Am I missing something?
from rq.
There's a package called django-rq-wrapper that implements autoreload and start multiple workers. I didn't test it, but seems pretty simple and not evasive since it brings a new management command called "rqworkers".
https://github.com/istrategylabs/django-rq-wrapper
from rq.
This would be a nice feature indeed +1
from rq.
i support this
from rq.
Can you manually send a signal to reload a worker?
from rq.
Any update on this?
from rq.
No one is working on this as far as I know. It would be nice to have this feature though. Django uses watchman to trigger reloads. I'd accept a PR that implements live reload functionality :)
from rq.
Thanks for the update. I'll see if I find the time to submit a PR. For now just went with using entr with ack (doesn't work if you add new files though):
ack -f | entr -r -s "rq worker -u redis://redis:6379"
from rq.
@honi THanks. Works well.
PSA: Don't use it in production. This eats HELLA memory. Like 100 mb (regular rq worker) vs 300 mb (this custom version)
from rq.
Correct! In production you should use the default rqworker management command.
from rq.
Like in #2 (comment) I made a simple test and it worked. Can someone send me an example so I can look at it.
from rq.
When you enqueue()
something, it creates a job using the reference for the function/callable (which can be both a string or a callable, if the latter, it will be converted to a string - basically the import path). This sets a couple of private attributes to the job (_func_name
and _instance
), those attributes will then be used by the job itself to getattr
or import
the relevant code (this happens on the func
property of the job).
What the worker does is basically just call the perform
method of the job, that's what actually executes the callable. So indeed, there shouldn't be a need to add a reload functionality, since the worker doesn't know the job before it actually pulls it from the queue.
If anyone has any issues with this, feel free to open a new issue so we can investigate. Closing it for now.
from rq.
Python will cache modules that have already been imported, and the default worker will fork new processes when executing tasks, so it will not be affected by module caching. If you use simple worker, there will be problems when making code changes
from rq.
Related Issues (20)
- Replace Redis with open source alternative? HOT 5
- Questions about Copilot + Open Source Software Hierarchy
- How do I log with INFO level from a job?
- no way to catch the redis.exceptions.ConnectionError on pubsub.get_message HOT 4
- Integrating OpenCV with FastAPI and Redis RQ Causes Task/Job Failures HOT 4
- Installing setproctitle >= 1.3.0 breaks RQ on MacOS HOT 1
- Job B depends on job A. A fails. Is there a parameter set that would result in B marked as failed?
- Using `@job` decorator and `func.delay()` causes subclass classmethods to not work correctly HOT 1
- Periodic High CPU with RQ 1.16.1 (not in RQ 1.12.0) HOT 1
- Orphaned "queued" job after shutdown/restart of worker
- [Function Request] Hope to support scheduler cron syntax HOT 3
- [bug] --with-scheduler The scheduled task with the queued name cannot be executed.
- Run Scheduler Immediately Upon `rqworker --with-scheduler` HOT 2
- Error "signal only works in main thread of the main interpreter" after a system failure HOT 1
- The @job decorator is not working as documented
- `import_attribute(...)` in `utils.py` swallows import errors in custom worker classes when running via the CLI. HOT 1
- [feature] WorkerPool with different configuration for queues HOT 2
- Use typing.Protocol to strictly enforce callback function signatures
- How to check why a job is killed? HOT 1
- Admin privileges required to boot RQ Worker
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rq.