Using web.py as non blocking http-server

后端 未结 4 1899
梦谈多话
梦谈多话 2021-02-05 11:30

while learning some basic programming with python, i found web.py. i got stuck with a stupid problem:

i wrote a simple console app with a main loop that proccesses items

相关标签:
4条回答
  • 2021-02-05 11:59

    I found a working solution. In a seperate module i create my webserver:

    import web
    import threading
    class MyWebserver(threading.Thread):
    
        def run (self):
            urls = ('/', 'MyWebserver')
            app = web.application(urls, globals())
            app.run()
    
        def POST ...
    

    In the main programm i just call

    MyWebserver().start()
    

    and than go on with whatever i want while having the webserver working in the background.

    0 讨论(0)
  • 2021-02-05 12:00

    Wouldn't is be simpler to re-write your main-loop code to be a function that you call over and over again, and then call that from the function that you pass to runsimple...

    It's guaranteed not to fully satisfy your requirements, but if you're in a rush, it might be easiest.

    0 讨论(0)
  • 2021-02-05 12:03

    or just use Tornado, a non-blocking webserver for Python that has an API similar to webpy - http://www.tornadoweb.org/

    0 讨论(0)
  • 2021-02-05 12:06

    I have also recently used Beanstalkd to queue up tasks that will run in a separate thread. Your web.py handler just drops a job into a pipe and a completely separate script executes it. You could have any number of these, and you get the benefits of advanced queue control, etc..

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