Skip to content

Instantly share code, notes, and snippets.

@arc279
Last active June 15, 2017 11:39
Show Gist options
  • Save arc279/480b71693c0f677d0e540e94812ccba5 to your computer and use it in GitHub Desktop.
Save arc279/480b71693c0f677d0e540e94812ccba5 to your computer and use it in GitHub Desktop.
常駐型で処理する
#!/usr/bin/env python
import os
import io
import click
import pandas as pd
from bottle import Bottle, request
app = Bottle()
@app.post('/resource')
def post():
print(request.forms.__dict__)
print(request.files.__dict__)
print(app.df0)
o = request.forms["out"]
f1 = io.TextIOWrapper(request.files.get("file1").file, encoding="utf-8")
df1 = pd.read_table(f1)
print(df1)
f2 = io.TextIOWrapper(request.files.get("file2").file, encoding="utf-8")
df2 = pd.read_table(f2)
print(df2)
dfm = pd.merge(pd.merge(app.df0, df1), df2)
print(dfm.to_csv(sep="\t"), file=open(o, "w"))
return "OK %s" % o
@click.command()
@click.option("--df0", default="z.tsv", type=click.File("r"))
@click.option("--port", default=8080, type=int)
@click.option("--pid", default="pid", type=str)
def cmd(df0, port, pid):
with open(pid, "w") as fp:
print(os.getpid(), file=fp)
app.df0 = pd.read_table(df0)
app.run(host='localhost', port=port, debug=True, reloader=True)
if __name__ == "__main__":
cmd()
#!/bin/bash
# sample file
cat <<EOD > z.tsv
k v0
aaa 111
ccc 222
ddd 333
EOD
cat <<EOD > a.tsv
k v1
aaa 123
bbb 234
ccc 345
EOD
cat <<EOD > b.tsv
k v2
aaa 555
bbb 666
ddd 777
EOD
# start server
./a.py &
test -r pid && kill $(cat pid)
trap 'kill $(cat pid); rm -f pid' EXIT
while true; do
nc -z localhost 8080 && break
echo waiting...
sleep 1
done
# proccessing
var=()
curl -X POST http://localhost:8080/resource \
-F "[email protected]" \
-F "[email protected]" \
-F "out=out1" &
var+=($!)
curl -X POST http://localhost:8080/resource \
-F "[email protected]" \
-F "[email protected]" \
-F "out=out2" &
var+=($!)
curl -X POST http://localhost:8080/resource \
-F "[email protected]" \
-F "[email protected]" \
-F "out=out3" &
var+=($!)
wait ${var[@]}
echo
echo "finish"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment