Skip to content
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

Switch between thread & global for app.backend #7961

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 28 additions & 5 deletions celery/app/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Actual App instance implementation."""
import inspect
import multiprocessing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as celery use billiard which is a fork of multiprocessing,, should we use multiprocessing here? can multiprocessing.current_process() imported from billiard?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did not realize it was a thing, will switch to billiard

Meanwhile fixed flake8 (cannot open another fork atm, committed here) PyCQA/flake8#1689

import os
import sys
import threading
Expand Down Expand Up @@ -229,6 +230,8 @@ def __init__(self, main=None, loader=None, backend=None,
**kwargs):

self._local = threading.local()
self._global = multiprocessing.current_process()
self._green = False

self.clock = LamportClock()
self.main = main
Expand Down Expand Up @@ -1249,14 +1252,34 @@ def amqp(self):
"""AMQP related functionality: :class:`~@amqp`."""
return instantiate(self.amqp_cls, app=self)

def inform_green(self, is_green):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a show-blocker, but I'd recommend adding a small unit test according to the code coverage warning

if is_green:
self._green = is_green

def _cached_or_create(self, name, factory, thread_safe):
"""Thread or process store

:param: thread_safe: func(created) => True/False
"""
cached = (getattr(self._local, name, None)
or getattr(self._global, name, None))
if cached is not None:
return cached

created = factory()
use_global = self._green or thread_safe(created)
setattr(self._global if use_global else self._local,
name, created)
return created

@property
def backend(self):
"""Current backend instance."""
try:
return self._local.backend
except AttributeError:
self._local.backend = new_backend = self._get_backend()
return new_backend
return self._cached_or_create(
'backend',
factory=self._get_backend,
thread_safe=lambda x: x.thread_safe,
)

@property
def conf(self):
Expand Down
3 changes: 3 additions & 0 deletions celery/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ class Backend:
#: Set to true if the backend is persistent by default.
persistent = True

#: Set to true if backend lib is thread-safe
thread_safe = False

retry_policy = {
'max_retries': 20,
'interval_start': 0,
Expand Down
2 changes: 2 additions & 0 deletions celery/backends/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ class RedisBackend(BaseKeyValueStoreBackend, AsyncBackendMixin):
GET, MGET, DEL, INCRBY, EXPIRE, SET, SETEX
"""

thread_safe = True

ResultConsumer = ResultConsumer

#: :pypi:`redis` client module.
Expand Down
2 changes: 2 additions & 0 deletions celery/worker/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ def setup_instance(self, queues=None, ready_callback=None, pidfile=None,

# Initialize bootsteps
self.pool_cls = _concurrency.get_implementation(self.pool_cls)
self.app.inform_green(self.pool_cls.is_green)

self.steps = []
self.on_init_blueprint()
self.blueprint = self.Blueprint(
Expand Down