Skip to content

Instantly share code, notes, and snippets.

@melin
Last active September 11, 2017 06:55
Show Gist options
  • Save melin/99e673d2d0a3c9dddc76bc38530397da to your computer and use it in GitHub Desktop.
Save melin/99e673d2d0a3c9dddc76bc38530397da to your computer and use it in GitHub Desktop.
##服务代码
```python
#!/usr/bin/env python2
#coding=utf-8
import tornado.ioloop
import tornado.web
from tornado.escape import json_encode
from pyspark.sql import SparkSession
import jedi
from tornado.options import define, options
class StatusHandler(tornado.web.RequestHandler):
def get(self):
self.write("ok")
class SparkSessionHandler(tornado.web.RequestHandler):
def post(self):
source = self.get_argument("source", default=None)
script = jedi.Interpreter(source, [{"sparkSession": SparkSession}])
completions = script.completions()
items = [];
for comp in completions:
if hasattr(comp._name.parent_context, '_context'):
context = comp._name.parent_context._context.name.string_name
else:
if hasattr(comp._name.parent_context, 'instance'):
context = comp._name.parent_context.instance.name.string_name
else:
context = comp._name.parent_context.name.string_name
items.append({'context': context, 'name': comp.name})
callback = self.get_argument('callback', default=None)
if callback :
jsonp = "{jsfunc}({json});".format(jsfunc=callback,
json=json_encode(items))
self.set_header('Content-Type', 'application/javascript')
self.write(jsonp)
else:
self.set_header('Content-Type', 'application/json')
self.write(json_encode(items))
class ScriptHandler(tornado.web.RequestHandler):
def post(self):
source = self.get_argument("source", default=None)
row = self.get_argument("row", default=None)
column = self.get_argument("column", default=None)
row = int(row)
column = int(column)
script = jedi.Script(source, row, column, '')
completions = script.completions()
items = [];
for comp in completions:
if hasattr(comp._name.parent_context, '_context'):
context = comp._name.parent_context._context.name.string_name
else:
if hasattr(comp._name.parent_context, 'instance'):
context = comp._name.parent_context.instance.name.string_name
else:
context = comp._name.parent_context.name.string_name
items.append({'context': context, 'name': comp.name})
callback = self.get_argument('callback', default=None)
if callback:
jsonp = "{jsfunc}({json});".format(jsfunc=callback,
json=json_encode(items))
self.set_header('Content-Type', 'application/javascript')
self.write(jsonp)
else:
self.set_header('Content-Type', 'application/json')
self.write(json_encode(items))
def make_app():
return tornado.web.Application([
(r"/ok.htm", StatusHandler),
(r"/sparkSession", SparkSessionHandler),
(r"/script", ScriptHandler),
])
define("port", default=9110, help="server port")
define("num_processes", default=1, help="bind cpu")
# http://beginman.cn/web/2015/04/06/jsonp-tornado/
# for i in `ps waux | grep "code-autocomplete" | awk '{ print $2 }'`; do kill -15 ${i}; done
if __name__ == "__main__":
tornado.options.options.logging = "warn"
tornado.options.parse_command_line()
app = make_app()
http_server = tornado.httpserver.HTTPServer(app)
http_server.bind(options.port)
http_server.start(options.num_processes)
print("server started, port {}".format(options.port))
tornado.ioloop.IOLoop.current().start()
```
## 启动脚本
```sh
#!/bin/bash
start() {
if [ -f ".autocomplete.pid" ]; then
echo "warn: App is already run, please check."
exit;
fi
STR=`netstat -an | grep "9110" | grep LISTEN`
if [ ! -z "$STR" ]; then
echo "warn: port is already used, please check."
exit;
fi
nohup ./code-autocomplete.py 2>&1 &
echo $! > .autocomplete.pid
pid=`cat .autocomplete.pid`
echo "APP $pid startup is success!"
}
stop() {
if [ ! -f ".autocomplete.pid" ]; then
echo "App process is not exist!"
return
fi
pid=`cat .autocomplete.pid`
for i in `ps waux | grep "code-autocomplete" | grep $pid | awk '{ print $2 }'`; do kill -15 ${i}; done
rm -rf .autocomplete.pid
echo "APP $pid shutdown is success!"
}
usage() {
echo "Usage: server.sh {start|stop|restart}"
exit 1;
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 5
start
;;
*)
echo $ACTION
usage
;;
esac
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment