17

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 from a queue in seperate threads. my goal is to use web.py to add items to my queue and report status of the queue via web request. i got this running as a module but can´t integrate it into my main app. my problem is when i start the http server with app.run() it blocks my main loop. also tried to start it with thread.start_new_thread but it still blocks. is there an easy way to run web.py´s integrated http server in the background within my app.

in the likely event that i am a victim of a fundamental missunderstanding, any attempt to clarify my error in reasoning would help ;.) ( please bear with me, i am a beginner :-)

1
  • You shouldn't answer in the question. Commented Feb 5, 2009 at 18:24

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm having problems killing this program with Ctrl+C, can anyone else confirm?
Be aware that you might have to add the initialisation of threading during the initialisation of you class like this: def __init__(self): threading.Thread.__init__(self).
@puk Set MyWebServer.daemon = True
1

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.

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.