小编Sha*_*n26的帖子

使用 Python3 在 AWS lambda 中进行多线程处理

我正在尝试在 AWS lambda 中实现多线程。这是一个示例代码,它定义了我试图在 lambda 中执行的原始代码的格式。

import threading
import time

def this_will_await(arg,arg2):
  print("Hello User")
  print(arg,arg2)

def this_should_start_then_wait():
  print("This starts")
  timer = threading.Timer(3.0, this_will_await,["b","a"])
  timer.start()
  print("This should execute")

this_should_start_then_wait()
Run Code Online (Sandbox Code Playgroud)

在我的本地机器上,此代码运行良好。我收到的输出是:

This starts
This should execute
.
.
.
Hello User
('b', 'a')
Run Code Online (Sandbox Code Playgroud)

那些 3 。表示它等待了 3 秒才完成执行。

现在,当我在 AWS lambda 中执行相同的操作时。我只收到

This starts
This should execute
Run Code Online (Sandbox Code Playgroud)

我认为它没有调用 this_will_await() 函数。

python multithreading amazon-web-services python-3.x aws-lambda

8
推荐指数
1
解决办法
7068
查看次数

在 RequestHandler 写入的情况下, Yield 和 Await 的不同行为(Tornado Web 框架)

我正在使用 RequestHandler 使用 Tornado Web 框架进行基于 Web 的调用。以前我使用的是 Tornado 5.1.1 版,它支持 @gen.coroutine 和 yield。

我正在将我的 tornado 版本移至 6.0.2,最近因为他们贬值了装饰协程,所以我将我的代码移至本机协程。

但我注意到装饰协程和原生协程之间的不同行为。

示例

  • 主要.py:
import tornado.ioloop as ioloop
import tornado.web as web
from RequestHandler import MainHandler

def main():
    application = web.Application([
        (r"/", MainHandler),
    ])
    application.listen(8080)

    ioloop.IOLoop.current().start()

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)
  • MainHandler ( Tornado 版本 5.0.2 )
import tornado.web
import tornado.gen

class MainHandler(tornado.web.RequestHandler):
    def initialize(self):
        self.data = "Hello World"

    @gen.coroutine
    def get(self):
        yield self.write(self.data)

    @gen.coroutine
    def post(self):
        yield self.write(self.data)

Run Code Online (Sandbox Code Playgroud)
  • MainHandler ( 版本 …

python asynchronous tornado python-3.x

5
推荐指数
1
解决办法
422
查看次数

WebSocket Handler on_close 方法 async 实现龙卷风

我正在使用 Tornado 6.0.2 版构建 Web 应用程序。我正在使用 WebSocket 处理程序来设置与客户端的连接。

示例服务器端实现:

from tornado import websocket
import connectionhandler


class WebSocketHandler(websocket.WebSocketHandler):
    def initialize(self, connectionhandler):
        self.connectionhandler = connectionhandler

    async def open(self):
        print("WebSocket opened.")
        await self.connectionhandler.connection_established_websocket()

    async def on_close(self):
        print("WebSocket closed.")
        await self.connectionhandler.connection_closed_websocket()

Run Code Online (Sandbox Code Playgroud)

示例客户端实现:

ws = websocket.create_connection("ws://localhost:80/ws?")
ws.close()
Run Code Online (Sandbox Code Playgroud)

当客户端建立连接时,它调用 open 方法并且一切正常。

但是当客户端关闭连接时,我收到错误 on_close 从未等待过。

当我删除本机协程时 on_close 方法正在工作。

题 :

如何为 on_close 方法添加本机协程或从 on_close() 调用异步方法?

python tornado websocket python-3.x

5
推荐指数
1
解决办法
602
查看次数