mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
fix: correctly handle asyncio.run when loop exists
This commit is contained in:
committed by
Jair Henrique
parent
81978659f1
commit
3fb62e0f9b
@@ -166,6 +166,22 @@ def async_vcr_send(cassette, real_send):
|
|||||||
return _inner_send
|
return _inner_send
|
||||||
|
|
||||||
|
|
||||||
|
def _run_async_function(sync_func, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
Safely run an asynchronous function from a synchronous context.
|
||||||
|
Handles both cases:
|
||||||
|
- An event loop is already running.
|
||||||
|
- No event loop exists yet.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
asyncio.get_running_loop()
|
||||||
|
except RuntimeError:
|
||||||
|
return asyncio.run(sync_func(*args, **kwargs))
|
||||||
|
else:
|
||||||
|
# If inside a running loop, create a task and wait for it
|
||||||
|
return asyncio.ensure_future(sync_func(*args, **kwargs))
|
||||||
|
|
||||||
|
|
||||||
def _sync_vcr_send(cassette, real_send, *args, **kwargs):
|
def _sync_vcr_send(cassette, real_send, *args, **kwargs):
|
||||||
vcr_request, response = _shared_vcr_send(cassette, real_send, *args, **kwargs)
|
vcr_request, response = _shared_vcr_send(cassette, real_send, *args, **kwargs)
|
||||||
if response:
|
if response:
|
||||||
@@ -174,7 +190,7 @@ def _sync_vcr_send(cassette, real_send, *args, **kwargs):
|
|||||||
return response
|
return response
|
||||||
|
|
||||||
real_response = real_send(*args, **kwargs)
|
real_response = real_send(*args, **kwargs)
|
||||||
asyncio.run(_record_responses(cassette, vcr_request, real_response, aread=False))
|
_run_async_function(_record_responses, cassette, vcr_request, real_response, aread=False)
|
||||||
return real_response
|
return real_response
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user