Skip to content

Commit

Permalink
Cythonize asynq_to_async
Browse files Browse the repository at this point in the history
  • Loading branch information
dkang-quora committed Aug 11, 2023
1 parent 1e9cd2c commit 9f25069
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
10 changes: 7 additions & 3 deletions asynq/asynq_to_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@


import asyncio
import cython
from typing import Any, Awaitable

from .batching import BatchItemBase
from .futures import ConstFuture

_asyncio_mode = 0
_asyncio_mode = cython.declare(cython.int, 0)


# asyncio_mode > 0 indicates that a synchronous call runs on an asyncio loop.
def is_asyncio_mode() -> bool:
@cython.ccall
@cython.inline
def is_asyncio_mode():
return _asyncio_mode > 0


Expand Down Expand Up @@ -52,6 +55,7 @@ async def resolve_awaitables(x: Any):
raise TypeError("Unknown structured awaitable type: ", type(x))


@cython.cclass
class AsyncioMode:
"""
This context manager is internally used by .asyncio() to indicate that we are in asyncio mode.
Expand All @@ -64,6 +68,6 @@ def __enter__(self):
global _asyncio_mode
_asyncio_mode += 1

def __exit__(self, *exeception_details):
def __exit__(self, exc_type, exc_value, tb):
global _asyncio_mode
_asyncio_mode -= 1
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

CYTHON_MODULES = [
"async_task",
"asynq_to_async",
"batching",
"contexts",
"_debug",
Expand Down

0 comments on commit 9f25069

Please sign in to comment.