Skip to content

Instantly share code, notes, and snippets.

@pbertera
Forked from fspot/bottlequeue.py
Last active October 11, 2018 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbertera/8b3faa4f6794640d57fb923c8ea9104a to your computer and use it in GitHub Desktop.
Save pbertera/8b3faa4f6794640d57fb923c8ea9104a 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 range(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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment