Created
June 7, 2013 08:20
-
-
Save fspot/5727795 to your computer and use it in GitHub Desktop.
Example app using bottle and multiprocessing for queuing long jobs and using several workers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding:utf-8 -*- | |
""" | |
Example app using bottle and multiprocessing for queuing long jobs | |
and using several workers. | |
""" | |
from multiprocessing import Process, Queue, cpu_count | |
from bottle import Bottle, run | |
import time | |
app = Bottle() | |
app.queue = Queue() | |
app.nb_workers = cpu_count() | |
def do_long_stuff(msg): | |
print ">> Long stuff to do with :", msg | |
time.sleep(3) | |
print "<< Long stuff done with :", msg | |
def pull_msgs(queue): | |
while True: | |
msg = queue.get() | |
if msg is None: | |
break | |
do_long_stuff(msg) | |
@app.route('/') | |
def index(): | |
return 'Hello !' | |
@app.route('/add/<msg>') | |
def add_message_to_queue(msg): | |
app.queue.put(msg) | |
return 'Hello ' + msg | |
@app.route('/exit') | |
def end(msg): | |
for i in xrange(app.nb_workers): | |
app.queue.put(None) | |
def main(): | |
for i in xrange(app.nb_workers): | |
msg_puller_process = Process(target=pull_msgs, args=[app.queue]) | |
msg_puller_process.start() | |
run(app, host='localhost', port=8080) | |
if __name__ == '__main__': | |
main() |
Exactly the example I was looking for, thanks !
Nice~ Thanks~
I know this is super late and a little of topic, but what if one was to integrate this with UWSGI where we no longer run the if __name__ == '__main__':
? When using UWSGI, it simply looks for a
app = application = bottle.Bottle()
and runs based on the application
object every time a request is performed. The work can't be put at the top level, so we need some way of running it 'once' when the application starts.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HTTP response are ignored?