-
-
Notifications
You must be signed in to change notification settings - Fork 287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Worker pool handles spawned process #655
Draft
chromium7
wants to merge
7
commits into
rq:master
Choose a base branch
from
chromium7:rqworker_pool_handle_spawned_process
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4d12ec3
Worker pool handles spawned process
chromium7 c9aaacf
Use disk for test DB
chromium7 a735ee3
Change test to use postgres
chromium7 ac0859f
Add postgres in requirements
chromium7 7cd58c9
Add no input
chromium7 c746169
Debug failing CI
chromium7 755d1d7
Set multiprocessing start method to fork
chromium7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,38 @@ | ||||
import django | ||||
from multiprocessing import Process, get_start_method | ||||
from typing import Any | ||||
|
||||
from rq.worker_pool import WorkerPool, run_worker | ||||
|
||||
|
||||
class DjangoWorkerPool(WorkerPool): | ||||
def get_worker_process( | ||||
self, | ||||
name: str, | ||||
burst: bool, | ||||
_sleep: float = 0, | ||||
logging_level: str = "INFO", | ||||
) -> Process: | ||||
"""Returns the worker process""" | ||||
return Process( | ||||
target=run_django_worker, | ||||
args=(name, self._queue_names, self._connection_class, self._pool_class, self._pool_kwargs), | ||||
kwargs={ | ||||
'_sleep': _sleep, | ||||
'burst': burst, | ||||
'logging_level': logging_level, | ||||
'worker_class': self.worker_class, | ||||
'job_class': self.job_class, | ||||
'serializer': self.serializer, | ||||
}, | ||||
name=f'Worker {name} (WorkerPool {self.name})', | ||||
) | ||||
|
||||
|
||||
def run_django_worker(*args: Any, **kwargs: Any) -> None: | ||||
# multiprocessing library default process start method may be | ||||
# `spawn` or `fork` depending on the host OS | ||||
if get_start_method() == 'spawn': | ||||
django.setup() | ||||
|
||||
run_worker(*args, **kwargs) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to @jackkinsella's comment, we need to use our own Line 150 in 025a53f
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW The version of this function we're using in production was.
The exception catching and logging was transient and didn't stop us from using a 16 worker queue to process a few 100k events per day
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
django.setup()
solution is likely better (but I don't understand it so can't offer feedback)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just checked production (Heroku - unix) and
get_start_method()
was "fork". I know that simply executingrun_worker()
will not work in that environment due to the psycog issues with the DB connection from pre-fork.(Maybe this explains the failing CI test?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I just realized that db connections are also closed pre-fork in the
rqworker
management command.django-rq/django_rq/management/commands/rqworker.py
Lines 95 to 96 in d09421d
I called
django.setup()
because the spawned process doesn't load the installed apps at all. This works fine when I manually tested it, but fails in unit test. Probably its becausedjango.setup()
is not setting up the project in testing environment which leads to the process setting up its own DB instance.I'm currently using this django code for parallel test runner as a reference. I'll try to update the PR in a day or two.
https://github.com/django/django/blob/53719d6b5b745dd99b1ab9315afb242f706ebbf1/django/test/runner.py#L424-L432