我创建了龙卷风服务器,它接受python和matlab代码并执行它。这是服务器代码。
from zmq.eventloop.zmqstream import ZMQStream
from zmq.eventloop import ioloop
ioloop.install()
from functools import partial
from tornado import web, gen, escape
from tornado import options, httpserver
from tornado.concurrent import Future
settings = dict()
settings['autoreload'] = True
settings['debug'] = True
from jupyter_client import MultiKernelManager
reply_futures = {}
kids = []
class MainHandler(web.RequestHandler):
def get(self):
self.write("Hello")
class ExecuteHandler(web.RequestHandler):
def get(self):
self.write("approaching execute")
def post(self):
data = escape.json_decode(self.request.body)
print data
self.application.execute_code(data)
class Application(web.Application):
def __init__(self):
handlers = []
handlers.append((r"/", MainHandler))
handlers.append((r"/execute", ExecuteHandler))
web.Application.__init__(self, handlers, **settings)
self.km = MultiKernelManager()
self.setup_kernels()
def setup_kernels(self):
matlab_kernel_id = self.km.start_kernel(kernel_name="matlab")
python_kernel_id = self.km.start_kernel(kernel_name="python")
self.matlab_kernel_id = matlab_kernel_id
self.python_kernel_id = python_kernel_id
matkab_kernel_client = self.km.get_kernel(matlab_kernel_id).client()
matkab_kernel_client.start_channels()
python_kernel_client = self.km.get_kernel(python_kernel_id).client()
python_kernel_client.start_channels()
self.matkab_kernel_client = matkab_kernel_client
self.python_kernel_client = python_kernel_client
matlab_iopub_stream = ZMQStream(matkab_kernel_client.iopub_channel.socket)
matlab_shell_stream = ZMQStream(matkab_kernel_client.shell_channel.socket)
python_iopub_stream = ZMQStream(python_kernel_client.iopub_channel.socket)
python_shell_stream = ZMQStream(python_kernel_client.shell_channel.socket)
matlab_iopub_stream.on_recv_stream(partial(self.reply_callback, matkab_kernel_client.session))
matlab_shell_stream.on_recv_stream(partial(self.reply_callback, matkab_kernel_client.session))
python_iopub_stream.on_recv_stream(partial(self.reply_callback, python_kernel_client.session))
python_shell_stream.on_recv_stream(partial(self.reply_callback, python_kernel_client.session))
def reply_callback(self, session, stream, msg_list):
idents, msg_parts = session.feed_identities(msg_list)
reply = session.deserialize(msg_parts)
if "stream" == reply["msg_type"]:
print reply["content"]["text"]
parent_id = reply['parent_header'].get('msg_id')
reply_future = reply_futures.get(parent_id)
if reply_future:
reply_future.set_result(reply)
def execute_code(self, data):
matlab_code = data['matlab_code']
python_code = data['python_code']
self.execute_matlab_then_python(matlab_code, python_code)
@gen.coroutine
def execute_matlab_then_python(self, matlab_code, python_code):
print "executing matlab code"
parent_id1 = self.matkab_kernel_client.execute(matlab_code)
f1 = reply_futures[parent_id1] = Future()
yield f1
print "executing python code"
parent_id2 = self.python_kernel_client.execute(python_code)
f2 = reply_futures[parent_id2] = Future()
yield f2
def shutdown_kernels(self):
self.km.get_kernel(self.matlab_kernel_id).cleanup_connection_file()
self.km.shutdown_kernel(self.matlab_kernel_id)
self.km.get_kernel(self.python_kernel_id).cleanup_connection_file()
self.km.shutdown_kernel(self.python_kernel_id)
if __name__ == '__main__':
options.parse_command_line()
app = Application()
server = httpserver.HTTPServer(app)
server.listen(8888)
try:
ioloop.IOLoop.current().start()
except KeyboardInterrupt:
print 'going down'
finally:
app.shutdown_kernels()
我用来访问的客户代码是,这里
import json
import requests
matlab_code = "a=magic(3);disp(a)"
python_code = "print 'Hello World!!'"
data = {}
data['matlab_code'] = matlab_code
data['python_code'] = python_code
r = requests.post('http://0.0.0.0:8888/execute', json.dumps(data))
我关心的是保持执行的顺序,这样python代码只有在matlab完成后才能执行。我使用jupyter_client来执行matlab/python代码。我在这里使用python27。问题是当我提交代码时,它抛出< code > type error:' NoneType ' object is not iterable 。这里有一个堆栈-跟踪它。
[E 160809 15:17:51 ioloop:633] Exception in callback None Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py", line 887, in start handler_func(fd_obj, events) File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 275, in null_wrapper return fn(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events self._handle_recv() File "/usr/local/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv self._run_callback(callback, msg) File "/usr/local/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback callback(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 275, in null_wrapper return fn(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 191, in self.on_recv(lambda msg: callback(self, msg), copy=copy) File "tornado_test.py", line 86, in reply_callback reply_future.set_result(reply) File "/usr/local/lib/python2.7/dist-packages/tornado/concurrent.py", line 276, in set_result self._set_done() File "/usr/local/lib/python2.7/dist-packages/tornado/concurrent.py", line 320, in _set_done for cb in self._callbacks: TypeError: 'NoneType' object is not iterable
我不明白这里有什么问题?
不幸的是,神秘的错误消息“'NoneType'对象不可迭代”意味着您在同一个 Future
对象上多次调用set_result
。我对 zmq 或 jupyter 内核接口了解不多,但我的猜测是,要么两个不同内核的执行方法返回的 ID 重叠,要么每次执行
调用(来自两个不同的流?
问题内容: 我是T-SQL命令的新手,所以我在SQL逻辑中找到了一个可以使用它并想要对其进行测试的地方,但无法弄清楚我应该如何使用它: 请提出正确的用法。谢谢! 问题答案: 您在帮助中了吗?这是一个简单的例子:
事件处理程序添加到GameGui.java类的GridPane中,从第82行开始。 基本上,我试图找到您在网格窗格中拖动这个对象的任何节点的索引。通过将SetonMouseEnter应用到gridpane中的每个节点,我可以在不拖动的情况下完成这一任务,但是当我试图利用SetonMouseDragEnter时,我什么也没有得到。 有人有办法让这个工作吗? 这些是我应用于GridPane中节点的事件
这是我的建筑。格雷德尔: 我试图找出问题所在,使用以下代码来分析问题: 结果是:
我在使用fs.readfile进行同步时遇到了一个问题,他们没有在第一次初始化,如果我再次尝试请求,那么我就有结果了。我知道我可以用promise和我从Kriskowal找到一些有用的东西。我试过但没有成功。我不太明白如何实施它。如果有人能帮忙,我将永远感激。 代码:
我在一个DB中有两个表(和),它们每个都有一个称为的相互列。 我当前使用以下代码仅从中导入一些数据(,): 如果我也想从导入数据(例如,名为和的列),那么我应该向该代码添加什么? 我的目标是拥有这些钥匙: 编辑: 编辑2: 仍然得到一个错误:
问题内容: 我想用Tornado运行一个用Flask编写的简单应用程序。我该怎么做呢?我想使用Python 2.7和最新的Tornado版本(4.2)。 问题答案: Flask文档用于描述如何执行此操作,但是由于以下性能说明而已被删除。除非所有异步代码都已经用Tornado编写,否则您不需要Tornado来提供Flask应用程序。 关于WSGI的Tornado文档也对此进行了描述。它们还包括一个重