Replies: 3 comments 13 replies
-
|
Why are you not using |
Beta Was this translation helpful? Give feedback.
-
|
I would start with some refactoring. Run the async runtime in your main task. That way you can use anyio's native thread support. I'd recommend not to directly use Also, you should your subscription using an (thus your |
Beta Was this translation helpful? Give feedback.
-
|
same problem import contextlib
import anyio
from anyio.from_thread import start_blocking_portal
async def func1() -> None:
print("1")
@contextlib.asynccontextmanager
async def func0() -> None:
print("0")
async with anyio.create_task_group() as tg:
tg.start_soon(func1)
print("2")
yield
print("3")
def main() -> None:
with start_blocking_portal() as portal:
portal.call(lambda: func0().__aenter__())
main() |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
My goal is to make async code available to sync code, where the async code is running in a different thread, because I will have async tasks witch run in the background. Therefore, I have two threads. One where the async event loop runs (
event_loop_thread) and the other one for the blocking world (MainThread).I chose to use the
BlockingPortalfor that. Here is the code how I do the setupNow I can call
run_asyncwith an async function and everything works. Except, when I have aTaskGroupwith background tasks. Imagine the following example. I have aSubscriptionobject that starts arenewtask after subscribing. In order to test this out, I created the following scriptBut after the first
run_asynccall, I get aRuntimeError: Attempted to exit a cancel scope that isn't the current tasks's current cancel scope.Is there a solution to my problem?
Beta Was this translation helpful? Give feedback.
All reactions