1.here is no current event loop in thread 'Thread-1'
First, you're getting AssertionError: There is no current event loop in thread 'Thread-1'.
because asyncio
requires each thread in your program to have its own event loop, but it will only automatically create an event loop for you in the main thread. So if you call asyncio.get_event_loop
once in the main thread it will automatically create a loop object and set it as the default for you, but if you call it again in a child thread, you'll get that error. Instead, you need to explicitly create/set the event loop when the thread starts:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
Once you've done that, you should be able to use get_event_loop()
in that specific thread.
It is possible to start an asyncio
event loop in a subprocess started via multiprocessing
:
import asyncio
from multiprocessing importProcess@asyncio.coroutine
def coro():print("hi")def worker():
loop = asyncio.get_event_loop()
loop.run_until_complete(coro())if __name__ =="__main__":
p =Process(target=worker)
p.start()
p.join()
2. TypeError: insert_data() takes 1 positional argument but 2 were given
class HandlerAsync():
async def insert_data(self, **kwargs): try: data = kwargs.get('kwargs') data1 = {"item": data.get('item'), "event": data.get('event'), "operator": data.get('operator'), "timestamp": time.strftime("%Y%m%d%H%M%S") } Log.objects.create(**data1) except Exception as e: print(e) async def handler(self, data): data = {"item": data.get('item'), "event": data.get('event'), "operator": data.get('operator'), "timestamp": time.strftime("%Y%m%d%H%M%S") } await self.insert_data(data) def execute(self, data): try: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete(self.handler(data)) loop.close() except Exception as e: print(traceback.print_exc()) 但是换一种参数写法,又正常
class HandlerAsync(): async def insert_data(self, data): try: log_data = {"item": data.get('item'), "event": data.get('event'), "operator": data.get('operator'), "timestamp": time.strftime("%Y%m%d%H%M%S") } Log.objects.create(**log_data) except Exception as e: print(e) async def handler(self, data): await self.insert_data(data) def execute(self, data): try: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete(self.handler(data)) loop.close() except Exception as e: print(traceback.print_exc())
相关推荐
网上async with和async for的中文资料比较少,我把PEP 492中的官方陈述翻译一下。 异步上下文管理器”async with” 异步上下文管理器指的是在enter和exit方法处能够暂停执行的上下文管理器。 为了实现这样的功能,...
Python3.5中async_await特性的实现.pdf
Python3.5中async_await特性的实现
python库,解压后可用。 资源全名:meilisearch_python_async-0.14.0-py3-none-any.whl
资源分类:Python库 所属语言:Python 资源全名:python-logstash-async-1.6.4.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
本文主要给大家简单讲解如何用 asyncio.Future 对象来封装文件的异步读写。有需要的小伙伴可以参考下
Python AsyncAPI 无需转换代码即可将规范转换为python代码的Python库。 AsyncAPI模式: 说明文件: 源代码: 主要特征 读取asyncapi规范并从中创建发布者和订阅者 支持使用数据类声明规格 提供创建订阅者的...
您可以使用@async_property ,你会与刚@property ,但异步函数。 class Foo : @ async_property async def remote_value ( self ): return await get_remote_value () 现在,属性remote_value返回一个等待的...
Xamarin.Android开发——异步加载数据更新主线程 //异步加载数据开始 doInBackground (); //异步加载数据开始end protected async void doInBackground () {
Python在3.5版本中引入了关于协程的语法糖async和await,所以下面这篇文章主要给大家介绍了关于Python从使用线程到使用async/await的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
主要介绍了python 异步async库的使用说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
注意:当前的rap API在后续版本中可能会发生重大变化 说唱第一版功能的想法来自 1.安装 pip install rap 2.快速入门 服务器 import asyncio from typing import AsyncIterator from rap . server import Server def...
使用AsyncBufferedConsumer,发送到Mixpanel API的事件将被批处理,然后在线程中刷新,而不会阻塞主线程。 这在响应时间很重要的请求/响应场景中非常有用。 该库最初是为在创建而创建的,并已在该生产环境中使用。...
数据量很大百万条记录,因此考虑到要用多线程并发执行,在写的过程中又遇到问题,我想统计所有子进程执行完毕总共的耗时,在第一个子进程创建前记录当前时间用System.currentTimeMillis()在后一个子进程结束后...
aiohttp 一个用于asyncio和Python的异步HTTP客户端/服务器
GDAX-Async-Python.zip script python
std::async异步调用函数,在某个时候以Args作为参数(可变长参数)调用Fn,无需等待Fn执行完成就可返回,返回结果是个std::future对象。Fn返回的值可通过std::future对象的get成员函数获取。一旦完成Fn的执行,共享...