简体   繁体   English

在Python中多线程的Twisted Websocket服务器和Swagger服务器?

[英]Autobahn Twisted websocket server and Swagger server multithreaded in python?

Can I run this swagger server 我可以运行这个摇摇欲坠的服务器

app = connexion.App(__name__, specification_dir='./swagger/')
app.app.json_encoder = JSONEncoder
app.add_api('swagger.yaml', arguments={'title': ''})

app.run(port=8080)

and this websocket server 和这个websocket服务器

from autobahn.twisted.websocket import WebSocketServerProtocol
import sys

from twisted.python import log
from twisted.internet import reactor
log.startLogging(sys.stdout)

from autobahn.twisted.websocket import WebSocketServerFactory
factory = WebSocketServerFactory()
factory.protocol = MyServerProtocol

reactor.listenTCP(9000, factory)
reactor.run()

in two different threads, in python? 在两个不同的线程,在Python中? Ultimately, when an id is posted to my swagger API, I want to forward this id to my websocket server and then send it through an existing matching connection to my websocket client. 最终,当一个id发布到我的swagger API时,我想将此id转发到我的websocket服务器,然后通过现有的匹配连接将其发送到我的websocket客户端。 So i thought that if I ran those two in two threads, maybe I could share this id as a variable. 所以我认为,如果我在两个线程中运行这两个线程,也许我可以将此id共享为变量。

If you choose to run the Twisted reactor in a non-main thread, you'll have to disable its signal handling (because Python only allows signal handlers in the main thread). 如果您选择在非主线程中运行Twisted反应器,则必须禁用其信号处理(因为Python仅允许在主线程中使用信号处理程序)。 This will disable some functionality but if you're not using that functionality this should be fine. 这将禁用某些功能,但是如果您不使用该功能,那应该没问题。

Just start the reactor this way: 只是这样启动反应堆:

reactor.run(installSignalHandlers=False)

This will let you run your Twisted code in a non-main thread (all in the same thread, so threadsafey issues should be minimized or eliminated). 这将使您可以在非主线程中运行Twisted代码(所有线程都在同一线程中,因此应最小化或消除线程安全性问题)。 That will, in turn, allow you to run your Swagger code in the main thread where it should work fine (because that's what you normally do). 这样一来,您就可以在主线程中可以正常运行Swagger代码的地方(因为这通常是您的工作)。

Alternatively, you could put the Swagger code in a non-main thread and Twisted in the main thread. 另外,您可以将Swagger代码放在非主线程中,将Twisted放在主线程中。 I don't know if there are steps you need to take to be able to run the Swagger code in a non-main thread. 我不知道是否需要采取一些步骤才能在非主线程中运行Swagger代码。

When it comes time to start passing data between the threads, you don't just want a variable that gets written by one and read by the other. 是时候开始在线程之间传递数据了,您不仅希望一个变量被一个人写入,而另一个变量被读取。 That would be a great step towards having non-thread-safe code. 那将是迈向拥有非线程安全代码的重要一步。 Instead, use one or more of the Twisted APIs for messaging between threads. 相反,请使用一个或多个Twisted API在线程之间进行消息传递。 For example, reactor.callFromThread is thread-safe (you're allowed to call it from any thread safely) and can be used to send a message from the Swagger thread to the Twisted reactor thread. 例如, reactor.callFromThread是线程安全的(允许您从任何线程安全地调用它),并且可用于将消息从Swagger线程发送到Twisted反应器线程。 Something like: 就像是:

reactor.callFromThread(process_received_id, id)

Now process_received_id(id) will execute in the Twisted reactor thread "soon" and you can do whatever you want, confident you don't have thread-safety issues because it's just as if , from the perspective of process_received_id , you're in a single-threaded application. 现在process_received_id(id)将在Twisted反应器线程“很快”执行,您可以做任何您想做的事情,确信您没有线程安全问题,因为从process_received_id的角度来看,就好像您在单线程应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python twisted多线程服务器 - python twisted multithreaded server Websocket连接与高速公路并在python中扭曲 - Websocket connection with autobahn and twisted in python 为什么Autobahn Twisted Websocket服务器没有完成与javascript客户端的握手? - Why is Autobahn Twisted Websocket server is not completing handshake with javascript client? WAMP websocket 服务器连接错误 Python 与高速公路 - WAMP websocket server connection error on Python with Autobahn Autobahn | Python Twisted服务器,用于检查API密钥并断开客户端连接 - Autobahn|Python Twisted server that checks API key and disconnects clients Autobahn twisted websocket 服务器不会向客户端发送消息,除非它收到来自客户端的消息 - Autobahn twisted websocket server does not send messages to client unless it receives a message from a client 如何使高速公路/扭曲的websocket服务器重新启动/异常后保持正常运行 - how to make autobahn / twisted websocket server restart / stay up after exception Python - 在一个单独的子进程或线程中运行Autobahn | Python asyncio websocket服务器 - Python - Running Autobahn|Python asyncio websocket server in a separate subprocess or thread Websocket:使用 Dart 客户端和 Autobahn-python 服务器 - Websocket: Working with Dart client and Autobahn-python server Autobahn Twisted WebSocket 内存泄漏 - Autobahn Twisted WebSocket memory leak
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM