How do you you run a Twisted application via Python (instead of via Twisted)?

后端 未结 6 1998
春和景丽
春和景丽 2020-12-24 08:48

I am working my way through learning Twisted, and have stumbled across something I\'m not sure I\'m terribly fond of - the \"Twisted Command Prompt\". I am fiddling around

相关标签:
6条回答
  • 2020-12-24 09:30

    I am successfully using the simple Twisted Web server on Windows for Flask web sites. Are others also successfully using Twisted on Windows, to validate that configuration?

    new_app.py
    
    if __name__ == "__main__":
        reactor_args = {}
    
        def run_twisted_wsgi():
            from twisted.internet import reactor
            from twisted.web.server import Site
            from twisted.web.wsgi import WSGIResource
    
            resource = WSGIResource(reactor, reactor.getThreadPool(), app)
            site = Site(resource)
            reactor.listenTCP(5000, site)
            reactor.run(**reactor_args)
    
        if app.debug:
            # Disable twisted signal handlers in development only.
            reactor_args['installSignalHandlers'] = 0
            # Turn on auto reload.
            import werkzeug.serving
            run_twisted_wsgi = werkzeug.serving.run_with_reloader(run_twisted_wsgi)
    
        run_twisted_wsgi()
    
    
    old_app.py
    
    if __name__ == "__main__":
        app.run()
    
    0 讨论(0)
  • 2020-12-24 09:32

    I haven't used twisted myself. However, you may try seeing if the twistd is a python file itself. I would take a guess that it is simply managing loading the appropriate twisted libraries from the correct path.

    0 讨论(0)
  • 2020-12-24 09:38

    I don't know if it's the best way to do this but what I do is instead of:

    application = service.Application("chatserver")
    internet.TCPServer(1025, factory).setServiceParent(application)
    

    you can do:

    from twisted.internet import reactor
    reactor.listenTCP(1025, factory)
    reactor.run()
    

    Sumarized if you want to have the two options (twistd and python):

    if __name__ == '__main__':
        from twisted.internet import reactor
        reactor.listenTCP(1025, factory)
        reactor.run()
    else:
        application = service.Application("chatserver")
        internet.TCPServer(1025, factory).setServiceParent(application)
    

    Hope it helps!

    0 讨论(0)
  • 2020-12-24 09:43

    On windows you can create .bat file with your command in it, use full paths, then just click on it to start up.

    For example I use:

    runfileserver.bat:
    C:\program_files\python26\Scripts\twistd.py -y C:\source\python\twisted\fileserver.tac
    
    0 讨论(0)
  • 2020-12-24 09:47

    Don't confuse "Twisted" with "twistd". When you use "twistd", you are running the program with Python. "twistd" is a Python program that, among other things, can load an application from a .tac file (as you're doing here).

    The "Twisted Command Prompt" is a Twisted installer-provided convenience to help out people on Windows. All it is doing is setting %PATH% to include the directory containing the "twistd" program. You could run twistd from a normal command prompt if you set your %PATH% properly or invoke it with the full path.

    If you're not satisfied with this, perhaps you can expand your question to include a description of the problems you're having when using "twistd".

    0 讨论(0)
  • 2020-12-24 09:48

    Maybe one of run or runApp in twisted.scripts.twistd modules will work for you. Please let me know if it does, it will be nice to know!

    0 讨论(0)
提交回复
热议问题