Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
python multithreading
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Sample multithreading with
Requirements: requests, bottle
To run:
$ python
To post data, open another command shell and type:
$ curl -X POST -H "Content-Type: application/json" -d '{"data":"test"}' -D-
import bottle
import json
import requests
from threading import Thread
POSTBACK_URL = '''/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'), data=result,
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, ))
#Immediately return a 200 response to the caller
return bottle.HTTPResponse(status=200, body="Complete process")
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment