本文整理汇总了Python中uvloop.new_event_loop方法的典型用法代码示例。如果您正苦于以下问题:Python uvloop.new_event_loop方法的具体用法?Python uvloop.new_event_loop怎么用?Python uvloop.new_event_loop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类uvloop
的用法示例。
在下文中一共展示了uvloop.new_event_loop方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: import uvloop [as 别名]
# 或者: from uvloop import new_event_loop [as 别名]
def run(coro, loop=None):
async def main_task():
pycurl_task = aio.ensure_future(curl_loop())
try:
r = await coro
finally:
pycurl_task.cancel()
with suppress(aio.CancelledError):
await pycurl_task
return r, pycurl_task
if loop is None:
loop = uvloop.new_event_loop()
# loop = aio.get_event_loop()
aio.set_event_loop(loop)
loop.set_exception_handler(exception_handler)
r, _ = loop.run_until_complete(main_task())
return r
示例2: pytest_configure
# 需要导入模块: import uvloop [as 别名]
# 或者: from uvloop import new_event_loop [as 别名]
def pytest_configure(config):
global LOOP_INIT
loop_name = config.getoption('--loop')
factory = {
"aioloop": asyncio.new_event_loop,
}
if uvloop is not None:
factory["uvloop"] = uvloop.new_event_loop
if loop_name:
if loop_name not in factory:
raise ValueError(
"{name} is not valid option".format(name=loop_name)
)
LOOP_INIT = factory[loop_name]
else:
LOOP_INIT = factory["aioloop"]
示例3: loop
# 需要导入模块: import uvloop [as 别名]
# 或者: from uvloop import new_event_loop [as 别名]
def loop(request, loop_type):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(None)
if uvloop and loop_type == 'uvloop':
loop = uvloop.new_event_loop()
else:
loop = asyncio.new_event_loop()
yield loop
if not loop._closed:
loop.call_soon(loop.stop)
loop.run_forever()
loop.close()
gc.collect()
asyncio.set_event_loop(None)
示例4: event_loop
# 需要导入模块: import uvloop [as 别名]
# 或者: from uvloop import new_event_loop [as 别名]
def event_loop(self):
q = asyncio.Queue()
for request_item in self.request_list:
q.put_nowait(request_item)
loop = uvloop.new_event_loop()
asyncio.set_event_loop(loop)
tasks = [self.handle_tasks(task_id, q) for task_id in range(self.coroutine_num)]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
示例5: run
# 需要导入模块: import uvloop [as 别名]
# 或者: from uvloop import new_event_loop [as 别名]
def run():
asyncio.set_event_loop(uvloop.new_event_loop())
server = app.create_server(host="0.0.0.0", port=80)
loop = asyncio.get_event_loop()
asyncio.ensure_future(server)
signal(SIGINT, lambda s, f: loop.stop())
try:
loop.run_forever()
except:
loop.stop()
示例6: loop_context
# 需要导入模块: import uvloop [as 别名]
# 或者: from uvloop import new_event_loop [as 别名]
def loop_context(loop_factory=asyncio.new_event_loop, fast=False):
"""A contextmanager that creates an event_loop, for test purposes.
Handles the creation and cleanup of a test loop.
"""
loop = setup_test_loop(loop_factory)
yield loop
teardown_test_loop(loop, fast=fast)
示例7: setup_test_loop
# 需要导入模块: import uvloop [as 别名]
# 或者: from uvloop import new_event_loop [as 别名]
def setup_test_loop(loop_factory=asyncio.new_event_loop):
"""Create and return an asyncio.BaseEventLoop instance.
The caller should also call teardown_test_loop, once they are done
with the loop.
"""
loop = loop_factory()
asyncio.set_event_loop(loop)
if sys.platform != "win32":
policy = asyncio.get_event_loop_policy()
watcher = asyncio.SafeChildWatcher()
watcher.attach_loop(loop)
policy.set_child_watcher(watcher)
return loop
示例8: pytest_configure
# 需要导入模块: import uvloop [as 别名]
# 或者: from uvloop import new_event_loop [as 别名]
def pytest_configure(config):
loops = config.getoption('--loop')
factories = {'pyloop': asyncio.new_event_loop}
if uvloop is not None: # pragma: no cover
factories['uvloop'] = uvloop.new_event_loop
if tokio is not None: # pragma: no cover
factories['tokio'] = tokio.new_event_loop
LOOP_FACTORIES.clear()
LOOP_FACTORY_IDS.clear()
if loops == 'all':
loops = 'pyloop,uvloop?,tokio?'
for name in loops.split(','):
required = not name.endswith('?')
name = name.strip(' ?')
if name in factories:
LOOP_FACTORIES.append(factories[name])
LOOP_FACTORY_IDS.append(name)
elif required:
raise ValueError(
"Unknown loop '%s', available loops: %s" % (
name, list(factories.keys())))
asyncio.set_event_loop(None)
示例9: publish
# 需要导入模块: import uvloop [as 别名]
# 或者: from uvloop import new_event_loop [as 别名]
def publish(self, channel_name='ready'):
signal(SIGINT, interrupt_handler)
try:
loop = uvloop.new_event_loop()
bot = Bot(token=getenv('TELEGRAM_KEY'), loop=loop)
task = loop.create_task(subscribe_and_listen(bot, channel_name))
loop.run_until_complete(task)
finally:
task.cancel()
loop.run_until_complete(bot.close())
loop.close()
示例10: launch_pubsub_task
# 需要导入模块: import uvloop [as 别名]
# 或者: from uvloop import new_event_loop [as 别名]
def launch_pubsub_task(func: Callable,
source='comments', sink='processed', subprocess=False):
async def pubsub_func():
r = redis.Redis(REDIS_HOST)
async for message in listen(source):
if message is StopAsyncIteration:
break
output = func(message['data'])
if output is not None:
await publish(r, sink, output)
def run_task():
loop = uvloop.new_event_loop()
task = loop.create_task(pubsub_func())
try:
loop.run_until_complete(task)
finally:
print('Stopping...')
task.cancel()
loop.stop()
loop.close()
signal(SIGINT, interrupt_handler)
if subprocess:
process = Process(target=run_task)
process.start()
else:
run_task()
示例11: publish
# 需要导入模块: import uvloop [as 别名]
# 或者: from uvloop import new_event_loop [as 别名]
def publish(self, channel_name='processed'):
signal(SIGINT, interrupt_handler)
try:
loop = uvloop.new_event_loop()
bot = Bot(token=getenv('TELEGRAM_KEY'), loop=loop)
task = loop.create_task(subscribe_and_listen(bot, channel_name))
loop.run_until_complete(task)
finally:
task.cancel()
loop.run_until_complete(bot.close())
loop.close()
示例12: get_event_loop
# 需要导入模块: import uvloop [as 别名]
# 或者: from uvloop import new_event_loop [as 别名]
def get_event_loop(self):
import asyncio
return asyncio.new_event_loop()
示例13: run
# 需要导入模块: import uvloop [as 别名]
# 或者: from uvloop import new_event_loop [as 别名]
def run():
asyncio.set_event_loop(uvloop.new_event_loop())
server = app.create_server(host="0.0.0.0", port=7777)
loop = asyncio.get_event_loop()
asyncio.ensure_future(server)
signal(SIGINT, lambda s, f: loop.stop())
try:
loop.run_forever()
except:
loop.stop()
示例14: new_event_loop
# 需要导入模块: import uvloop [as 别名]
# 或者: from uvloop import new_event_loop [as 别名]
def new_event_loop():
"""Get the fastest loop available.
"""
try:
import uvloop
return uvloop.new_event_loop()
except ImportError as err:
utils.log_to_stderr().warn(str(err))
return asyncio.new_event_loop()
示例15: _run_loop
# 需要导入模块: import uvloop [as 别名]
# 或者: from uvloop import new_event_loop [as 别名]
def _run_loop(self, debug):
self.loop = loop = new_event_loop()
loop._tid = get_ident()
# FIXME: causes error with a thread safety check in Future.call_soon()
# called from Future.add_done_callback() - stdlib needs a patch?
if debug:
loop.set_debug(debug)
logging.getLogger('asyncio').setLevel(logging.DEBUG)
asyncio.set_event_loop(loop)
self.loop.run_forever()