forked from celery/celery
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a concurrency model with ThreadPoolExecutor (celery#5099)
* Add a concurrency model with ThreadPoolExecutor * thread model test for pypy
- Loading branch information
Showing
13 changed files
with
106 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Thread execution pool.""" | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
import sys | ||
|
||
from concurrent.futures import wait, ThreadPoolExecutor | ||
from .base import BasePool, apply_target | ||
|
||
__all__ = ('TaskPool',) | ||
|
||
|
||
class ApplyResult(object): | ||
def __init__(self, future): | ||
self.f = future | ||
self.get = self.f.result | ||
|
||
def wait(self, timeout=None): | ||
wait([self.f], timeout) | ||
|
||
|
||
class TaskPool(BasePool): | ||
"""Thread Task Pool.""" | ||
|
||
body_can_be_buffer = True | ||
signal_safe = False | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(TaskPool, self).__init__(*args, **kwargs) | ||
|
||
# from 3.5, it is calculated from number of CPUs | ||
if (3, 0) <= sys.version_info < (3, 5) and self.limit is None: | ||
self.limit = 5 | ||
|
||
self.executor = ThreadPoolExecutor(max_workers=self.limit) | ||
|
||
def on_stop(self): | ||
self.executor.shutdown() | ||
super(TaskPool, self).on_stop() | ||
|
||
def on_apply(self, target, args=None, kwargs=None, callback=None, | ||
accept_callback=None, **_): | ||
f = self.executor.submit(apply_target, target, args, kwargs, | ||
callback, accept_callback) | ||
return ApplyResult(f) | ||
|
||
def _get_info(self): | ||
return { | ||
'max-concurrency': self.limit, | ||
'threads': len(self.executor._threads) | ||
# TODO use a public api to retrieve the current number of threads | ||
# in the executor when available. (Currently not available). | ||
} |
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,11 @@ | ||
============================================================= | ||
``celery.concurrency.thread`` | ||
============================================================= | ||
|
||
.. contents:: | ||
:local: | ||
.. currentmodule:: celery.concurrency.thread | ||
|
||
.. automodule:: celery.concurrency.thread | ||
:members: | ||
:undoc-members: |
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 @@ | ||
futures>=3.1.1; python_version < '3.0' |
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,26 @@ | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
import operator | ||
import pytest | ||
|
||
from celery.utils.functional import noop | ||
|
||
|
||
class test_thread_TaskPool: | ||
|
||
def test_on_apply(self): | ||
from celery.concurrency import thread | ||
x = thread.TaskPool() | ||
x.on_apply(operator.add, (2, 2), {}, noop, noop) | ||
|
||
def test_info(self): | ||
from celery.concurrency import thread | ||
x = thread.TaskPool() | ||
assert x.info | ||
|
||
def test_on_stop(self): | ||
from celery.concurrency import thread | ||
x = thread.TaskPool() | ||
x.on_stop() | ||
with pytest.raises(RuntimeError): | ||
x.on_apply(operator.add, (2, 2), {}, noop, noop) |