This commit is contained in:
yumoqing 2025-11-05 14:17:40 +08:00
parent bc0de26a18
commit 6388f44e80
2 changed files with 25 additions and 2 deletions

View File

@ -6,6 +6,29 @@ import concurrent
from functools import wraps
from functools import wraps
def schedule_once(seconds, coro, *args, **kw):
loop = asyncio.get_running_loop()
if inspect.iscoroutinefunction(coro):
print(f'{coro} is a coroutine')
x = loop.call_later(seconds, lambda: asyncio.create_task(coro(*args, **kw)))
else:
print(f'{coro} is a normal function')
x = loop.call_later(seconds, coro, *args, **kw)
return x
def schedule_interval(seconds, coro, *args, **kw):
loop = asyncio.get_running_loop()
"""在 asyncio 中实现 call_interval"""
def wrapper():
if inspect.iscoroutinefunction(coro):
loop.create_task(coro(*args, **kw))
else:
coro(*args, **kw)
return loop.call_later(seconds, wrapper)
handle = loop.call_later(seconds, wrapper)
return handle
def get_event_loop():
try:
return asyncio.get_runnung_loop()
@ -78,6 +101,6 @@ if __name__ == '__main__':
await asyncio.wait(g)
print('aaaaaaaaaaaaaaaaaaa')
loop = asyncio.get_event_loop()
loop = get_event_loop()
loop.run_until_complete(run())

View File

@ -1,6 +1,6 @@
[metadata]
name=appPublic
version = 5.2.11
version = 5.3.0
description = A set of base public functions or class
author = "yu moqing"
author_email = "yumoqing@gmail.com"