简体   繁体   中英

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

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? 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. So i thought that if I ran those two in two threads, maybe I could share this id as a variable.

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). 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). 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).

Alternatively, you could put the Swagger code in a non-main thread and Twisted in the main thread. 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.

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM