mirror of
https://github.com/ParisNeo/lollms.git
synced 2024-12-20 13:13:16 +00:00
added async execution methods
This commit is contained in:
parent
c7ca64c5c8
commit
d0368bdb2d
@ -30,6 +30,12 @@ import asyncio
|
||||
|
||||
import ctypes
|
||||
|
||||
def is_asyncio_loop_running():
|
||||
try:
|
||||
return asyncio.get_event_loop().is_running()
|
||||
except RuntimeError: # This gets raised if there's no running event loop
|
||||
return False
|
||||
|
||||
def run_async(func):
|
||||
"""
|
||||
run_async(func) -> None
|
||||
@ -42,10 +48,13 @@ def run_async(func):
|
||||
Returns:
|
||||
None: Nothing is returned since the function is meant to perform side effects.
|
||||
"""
|
||||
try:
|
||||
return asyncio.run(func)
|
||||
except:
|
||||
return func
|
||||
if is_asyncio_loop_running():
|
||||
# We're in a running event loop, so we can call the function with asyncio.create_task
|
||||
asyncio.create_task(func())
|
||||
else:
|
||||
# We're not in a running event loop, so we need to create one and run the function in it
|
||||
asyncio.run(func())
|
||||
|
||||
def terminate_thread(thread):
|
||||
if thread:
|
||||
if not thread.is_alive():
|
||||
|
Loading…
Reference in New Issue
Block a user