Skip to content

Commit

Permalink
Revert "simplified sync decorator"
Browse files Browse the repository at this point in the history
This reverts commit 51f1d81.
  • Loading branch information
sacOO7 committed Sep 29, 2023
1 parent 51f1d81 commit 0172888
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions ably/decorator/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ def wrapper(*args, **kwargs):
pass
ably_eventloop: events = AblyEventLoop.current().loop

# Handle calls from ably_eventloop on the same loop, return awaitable
if caller_eventloop is not None and caller_eventloop == ably_eventloop:
return ably_eventloop.create_task(fn(*args, **kwargs))

# Handle calls from external eventloop, post them on ably_eventloop
future = asyncio.run_coroutine_threadsafe(fn(*args, **kwargs), ably_eventloop)
if caller_eventloop is not None and caller_eventloop.is_running():
return asyncio.wrap_future(future)

# If called from regular function, return blocking result
return future.result()

return wrapper
res = fn(*args, **kwargs)
if asyncio.iscoroutine(res):
# Handle calls from ably_eventloop on the same loop, return awaitable
if caller_eventloop is not None and caller_eventloop == ably_eventloop:
return ably_eventloop.create_task(res)

# Handle calls from external eventloop, post them on ably_eventloop
future = asyncio.run_coroutine_threadsafe(res, ably_eventloop)
if caller_eventloop is not None and caller_eventloop.is_running():
return asyncio.wrap_future(future)

# If called from regular function, return blocking result
return future.result()
return res

return wrapper

0 comments on commit 0172888

Please sign in to comment.