sniffio support

This commit is contained in:
Mattia Giambirtone 2023-06-16 14:40:23 +02:00 committed by nocturn9x
parent 422304fcd9
commit 6f3394d7d6
Signed by: nocturn9x
GPG Key ID: 8270F9F467971E59
3 changed files with 16 additions and 0 deletions

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
sniffio==1.3.0

View File

@ -18,6 +18,7 @@ from collections import deque
from typing import Callable, Coroutine, Any
from functools import partial
import signal
import sniffio
class FIFOKernel(BaseKernel):
@ -285,6 +286,7 @@ class FIFOKernel(BaseKernel):
from tasks
"""
old_name, sniffio.thread_local.name = sniffio.thread_local.name, "structio"
try:
func()
except StopIteration as ret:
@ -324,6 +326,8 @@ class FIFOKernel(BaseKernel):
task.state = TaskState.CRASHED
self.event("on_exception_raised", task)
self.on_error(task)
finally:
sniffio.thread_local.name = old_name
def release_resource(self, resource: FdWrapper):
self.io_manager.release(resource)

11
tests/sniffio_test.py Normal file
View File

@ -0,0 +1,11 @@
import sniffio
import structio
async def main():
backend = sniffio.current_async_library()
assert backend == "structio"
print(f"[main] Current async backend: {backend}")
structio.run(main)