Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x micropython中的多方法异步IO_Python 3.x_Python Asyncio_Micropython - Fatal编程技术网

Python 3.x micropython中的多方法异步IO

Python 3.x micropython中的多方法异步IO,python-3.x,python-asyncio,micropython,Python 3.x,Python Asyncio,Micropython,当我运行以下代码时,它会运行并打印(“侦听,将应用程序连接到”),并作为web服务器等待请求。在web服务器模式下,我希望LED闪烁,这就是我应用asyncio的原因 然而,除非它收到任何请求(当为True:在web服务器中循环时激活),否则LED不会响应。我尝试了很多方法,但在web服务器模式下找不到切换LED的方法。您可以在下面的代码中看到关于wait asyncio.sleep(20)的注释: import uasyncio as asyncio from machine import P

当我运行以下代码时,它会运行并打印(“侦听,将应用程序连接到”),并作为web服务器等待请求。在web服务器模式下,我希望LED闪烁,这就是我应用
asyncio
的原因

然而,除非它收到任何请求(当为True:在web服务器中循环时激活
),否则LED不会响应。我尝试了很多方法,但在web服务器模式下找不到切换LED的方法。您可以在下面的代码中看到关于
wait asyncio.sleep(20)
的注释:

import uasyncio as asyncio
from machine import Pin
import time

LED_PIN = 13
led = Pin(LED_PIN, Pin.OUT, value=1)

async def toggle():
    while True:
        await asyncio.sleep_ms(500)
        led.value(not led.value()) # toggling        

async def webServer(ipAddress):
    s = socket.socket()
    ai = socket.getaddrinfo(ipAddress, 8080)
    print("Bind address info:", ai)
    addr = ai[0][-1]
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind(addr)
    s.listen(2)
    print("Listening, connect your APP to http://%s:8080/" % ipAddress)

    counter = 0
    # await asyncio.sleep(20) # !! if i applied await here, LED toggling 20 secs but web server does not accept any request because "while True" below is not activated during 20 secs.
    while True:
        res = s.accept()
        client_sock = res[0]
        client_addr = res[1]
        print("Client address:", client_addr)
        print("Client socket:", client_sock)

        req = client_sock.recv(1024)
        print("Payload: %s" % req.decode())
        client_sock.send(CONTENT % counter)
        client_sock.close()
        counter += 1
        print()

loop = asyncio.get_event_loop()
loop.create_task(toggle())
loop.create_task(webServer('192.168.4.1'))
loop.run_forever()

您的
webServer
async函数不是真正的异步函数,因为它使用阻塞IO。至少,您需要将套接字设置为非阻塞模式并使用套接字操作,或者更好地,您应该使用它来实现异步网络服务器


参见asyncio文档或示例。

循环创建任务(asyncio.start\u服务器(句柄\u客户端,(ap\u if.ifconfig()[0]),8080))&异步定义句柄\u客户端(读写器)
解决了问题,谢谢。只有一个问题,它没有覆盖打开的连接。只有在重置后,它才会进入web服务器模式。在sockets方法中,
s.setsockopt(socket.SOL\u socket,socket.SO\u REUSEADDR,1)
解决了这个问题,但我在asyncio中找不到一个方法,如何纠正它。@Sunrise17尝试将
reuse\u addr=True
传递到
启动\u服务器
。(这应该是POSIX OS es下的默认设置。)从移动应用程序(URLSession.shared.dataTask(with:request))向esp8266上安装的asyncio服务器发出如下5或6次请求后,服务器在一段时间后超时<代码>请求路径:/Content-Length:22请求头:主机:0.0.0.0:10000内容类型:应用程序/x-www-form-urlencoded连接:keep-alive-Accept://*用户代理:Chroma-Accept语言:Content-Length:22接受编码:gzip,deflate请求负载:{“COLOR”:[255,21226]}
@Sunrise17关于这个问题,也许你应该问一个不同的问题,举一个简单的例子等等。它与asyncio server有关,无论如何,我会提出一个新问题。我想知道你的嵌入式板名是什么?提前谢谢。