Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
python multithreading
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Sample multithreading with bottle.py
Requirements: requests, bottle
To run:
$ python app.py
To post data, open another command shell and type:
$ curl -X POST -H "Content-Type: application/json" -d '{"data":"test"}' http://127.0.0.1:8080/process -D-
"""
import bottle
import json
import requests
from threading import Thread
POSTBACK_URL = 'http://127.0.0.1:8080/postprocessed'
@bottle.post('/postprocessed')
def postprocessed_handle():
print('Received data at postprocessed')
return bottle.HTTPResponse(status=200, body="Complete postprocessed")
def process_data(data):
# do processing...
result = json.dumps(data)
print('Finished processing')
requests.post(POSTBACK_URL, data=result,
headers={'Content-Type':'application/json'})
@bottle.post('/process')
def process_handle():
data = bottle.request.json.get('data', False)
print('Received data at process: ' + data)
if not data:
return bottle.HTTPResponse(status=400, body="Missing data")
#Spawn thread to process the data
t = Thread(target=process_data, args=(data, ))
t.start()
#Immediately return a 200 response to the caller
return bottle.HTTPResponse(status=200, body="Complete process")
if __name__ == '__main__':
bottle.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment