本文整理匯總了Python中asyncio._get_running_loop方法的典型用法代碼示例。如果您正苦於以下問題:Python asyncio._get_running_loop方法的具體用法?Python asyncio._get_running_loop怎麽用?Python asyncio._get_running_loop使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類asyncio
的用法示例。
在下文中一共展示了asyncio._get_running_loop方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: run_main
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def run_main(main):
"""Main entrypoint for asyncio tasks.
This differs from `asyncio.run` in one key way - the main task is cancelled
*first*, then any outstanding tasks are cancelled (and logged, remaining
tasks are indicative of bugs/failures).
"""
if asyncio._get_running_loop() is not None:
raise RuntimeError("Cannot be called from inside a running event loop")
main_task = None
loop = asyncio.new_event_loop()
try:
asyncio.set_event_loop(loop)
main_task = loop.create_task(main)
return loop.run_until_complete(main_task)
finally:
try:
if main_task is not None:
loop.run_until_complete(cancel_task(main_task))
_cancel_all_tasks(loop)
loop.run_until_complete(loop.shutdown_asyncgens())
finally:
asyncio.set_event_loop(None)
loop.close()
示例2: test_get_event_loop_returns_running_loop
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def test_get_event_loop_returns_running_loop(self):
class Policy(asyncio.DefaultEventLoopPolicy):
def get_event_loop(self):
raise NotImplementedError
loop = None
old_policy = asyncio.get_event_loop_policy()
try:
asyncio.set_event_loop_policy(Policy())
loop = asyncio.new_event_loop()
self.assertIs(asyncio._get_running_loop(), None)
async def func():
self.assertIs(asyncio.get_event_loop(), loop)
self.assertIs(asyncio._get_running_loop(), loop)
loop.run_until_complete(func())
finally:
asyncio.set_event_loop_policy(old_policy)
if loop is not None:
loop.close()
self.assertIs(asyncio._get_running_loop(), None)
示例3: get_running_loop
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def get_running_loop():
"""Gets the currently running event loop
Uses asyncio.get_running_loop() if available (Python 3.7+) or a backported
version of the same function in 3.5/3.6.
"""
try:
loop = asyncio.get_running_loop()
except AttributeError:
loop = asyncio._get_running_loop()
if loop is None:
raise RuntimeError("no running event loop")
return loop
示例4: acquire_loop
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def acquire_loop(running: bool = False) -> asyncio.AbstractEventLoop:
"""Gracefully acquire a loop.
The function tries to get an event loop via :func:`asyncio.get_event_loop`.
On fail, returns a new loop using :func:`asyncio.new_event_loop`.
Parameters
----------
running: :class:`bool`
Indicates if the function should get a loop that is already running.
"""
try:
loop = asyncio._get_running_loop()
except Exception: # an error might occur actually
loop = None
if running and loop is not None:
return loop
else:
try:
loop = asyncio.get_event_loop()
if loop.is_running() and not running:
# loop is running while we have to get the non-running one,
# let us raise an error to go into <except> clause.
raise ValueError("Current event loop is already running.")
except Exception:
loop = asyncio.new_event_loop()
return loop
示例5: _get_running_loop
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def _get_running_loop():
return _running_loop._loop
示例6: _get_event_loop
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def _get_event_loop():
current_loop = _get_running_loop()
if current_loop is not None:
return current_loop
return asyncio.events.get_event_loop_policy().get_event_loop()
示例7: _get_running_loop
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def _get_running_loop():
return _running_loop._loop
示例8: _get_event_loop
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def _get_event_loop():
current_loop = _get_running_loop()
if current_loop is not None:
return current_loop
return asyncio.events.get_event_loop_policy().get_event_loop()
示例9: close
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def close(self) -> None:
"""
關閉天勤接口實例並釋放相應資源
Example::
# m1901開多3手
from tqsdk import TqApi
from contextlib import closing
with closing(TqApi()) as api:
api.insert_order(symbol="DCE.m1901", direction="BUY", offset="OPEN", volume=3)
"""
if self._loop.is_closed():
return
if self._loop.is_running():
raise Exception("不能在協程中調用 close, 如需關閉 api 實例需在 wait_update 返回後再關閉")
elif asyncio._get_running_loop():
raise Exception(
"TqSdk 使用了 python3 的原生協程和異步通訊庫 asyncio,您所使用的 IDE 不支持 asyncio, 請使用 pycharm 或其它支持 asyncio 的 IDE")
# 總會發送 serial_extra_array 數據,由 TqWebHelper 處理
for _, serial in self._serials.items():
self._process_serial_extra_array(serial)
self._run_until_idle() # 由於有的處於 ready 狀態 task 可能需要報撤單, 因此一直運行到沒有 ready 狀態的 task
for task in self._tasks:
task.cancel()
while self._tasks: # 等待 task 執行完成
self._run_once()
self._loop.run_until_complete(self._loop.shutdown_asyncgens())
self._loop.close()
_clear_logs() # 清除過期日誌文件
示例10: sync_wrapper
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def sync_wrapper(coro):
def inner_sync_wrapper(self, *args, **kwargs):
start_new_loop = None
# try to get the running loop
# `get_running_loop()` is new in Python 3.7, fall back on privateinternal for 3.6
try:
get_running_loop = asyncio.get_running_loop
except AttributeError:
get_running_loop = asyncio._get_running_loop
# If there is no running loop we will need to start a new one and run it to completion
try:
if get_running_loop():
start_new_loop = False
else:
start_new_loop = True
except RuntimeError:
start_new_loop = True
if start_new_loop is True:
f = asyncio.ensure_future(coro(self, *args, **kwargs))
asyncio.get_event_loop().run_until_complete(f)
f = f.result()
else:
# don't use create_task. It's python3.7 only
f = asyncio.ensure_future(coro(self, *args, **kwargs))
return f
return inner_sync_wrapper
# Monkey patch in our sync_wrapper