Skip to content

Commit

Permalink
Removed unnecessary coroutine check for decorater sync
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Sep 29, 2023
1 parent 0a71845 commit 7ed3ca9
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions ably/decorator/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def optional_sync(fn):
'''
Enables async function to be used as both sync and async function.
Also makes async/sync workflow thread safe.
This decorator should only be used on async methods/coroutines.
'''
import asyncio

Expand All @@ -34,12 +35,9 @@ def wrapper(*args, **kwargs):
except:
pass
ably_eventloop = get_custom_event_loop()
res = fn(*args, **kwargs)
if asyncio.iscoroutine(res):
future = asyncio.run_coroutine_threadsafe(res, ably_eventloop)
if caller_eventloop is not None and caller_eventloop.is_running():
return asyncio.wrap_future(future)
return future.result()
return res
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)
return future.result()

return wrapper

0 comments on commit 7ed3ca9

Please sign in to comment.