mirror of https://gitlab.com/litecord/litecord.git
don't use copy_current_app_context
in some scenarios, the app context might not be there yet, it's easier to use the global app instance and embed it in JobManager instead
This commit is contained in:
parent
bdf7d875ba
commit
88d6a19415
|
|
@ -20,12 +20,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
import asyncio
|
||||
from typing import Any
|
||||
|
||||
from quart.ctx import copy_current_app_context
|
||||
from logbook import Logger
|
||||
|
||||
log = Logger(__name__)
|
||||
|
||||
|
||||
class EmptyContext:
|
||||
async def __aenter__(self):
|
||||
pass
|
||||
|
||||
async def __aexit__(self, _typ, _value, _traceback):
|
||||
pass
|
||||
|
||||
|
||||
class JobManager:
|
||||
"""Background job manager.
|
||||
|
||||
|
|
@ -34,8 +41,9 @@ class JobManager:
|
|||
its own internal list of jobs.
|
||||
"""
|
||||
|
||||
def __init__(self, loop=None):
|
||||
def __init__(self, *, loop=None, context_func=None):
|
||||
self.loop = loop or asyncio.get_event_loop()
|
||||
self.context_function = context_func or EmptyContext
|
||||
self.jobs = []
|
||||
|
||||
async def _wrapper(self, coro):
|
||||
|
|
@ -50,9 +58,9 @@ class JobManager:
|
|||
def spawn(self, coro):
|
||||
"""Spawn a given future or coroutine in the background."""
|
||||
|
||||
@copy_current_app_context
|
||||
async def _ctx_wrapper_bg() -> Any:
|
||||
return await coro
|
||||
async with self.context_function():
|
||||
return await coro
|
||||
|
||||
task = self.loop.create_task(self._wrapper(_ctx_wrapper_bg()))
|
||||
self.jobs.append(task)
|
||||
|
|
|
|||
Loading…
Reference in New Issue