Skip to content

Instantly share code, notes, and snippets.

@fspot
Created June 7, 2013 08:20
Show Gist options
  • Save fspot/5727795 to your computer and use it in GitHub Desktop.
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.
#!/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()
@6noque
Copy link

6noque commented May 28, 2015

Exactly the example I was looking for, thanks !

@dabeike
Copy link

dabeike commented Sep 6, 2015

Nice~ Thanks~

@jhallard
Copy link

jhallard commented Jan 4, 2018

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