Skip to content

Instantly share code, notes, and snippets.

@jackhftang
Created September 4, 2017 03:31
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 jackhftang/2bb2bde6f601362a970c73cc7072f3ec to your computer and use it in GitHub Desktop.
Save jackhftang/2bb2bde6f601362a970c73cc7072f3ec to your computer and use it in GitHub Desktop.
from bottle import run, post, request
from threading import Thread
from queue import Queue
import datetime
import requests
import shutil
import tempfile
from os.path import join
import time
HOST = '0.0.0.0'
PORT = 22222
ENDPOINT = 'https://127.0.0.1/api/v0/finish'
def log(*arg):
t = '{0:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now())
print(t, *arg)
## job queue
jobQueue = Queue()
## worker
def worker():
print('Running worker')
while True:
job = jobQueue.get(block=True)
log('processing job:', job)
try:
process(job)
except:
pass
log('finished job:', job)
## modify this
def predict(imgPath):
time.sleep(1)
return [
{'class': 'a', 'score': 0.90},
{'class': 'b', 'score': 0.48},
{'class': 'c', 'score': 0.38},
{'class': 'e', 'score': 0.32}
]
def process(job):
r = requests.get(job['image_url'], verify=False, timeout=10, stream=True)
print(r.status_code)
if r.status_code == 200:
tempdir = tempfile.gettempdir()
path = join(tempdir, 'image')
print(path)
with open(path, 'wb') as f:
r.rawdecode_content = True
shutil.copyfileobj(r.raw, f)
result = predict(path)
requests.post(ENDPOINT, json={
'job_id': job['job_id'],
'result': result
})
else:
## post without result = failed
request.post(ENDPOINT, json={
'job_id': job['job_id']
})
## background thread for processing
Thread(target=worker).start()
@post('/')
def index():
json = request.json
log('received job:', json)
jobQueue.put(json)
print(f'Listening on port {PORT}')
run(host=HOST, port=PORT, reloader=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment