程序被龙卷风卡住
Program gets stuck with tornado
我正在尝试通过本地网络 Raspberry Pi 将纯文本发送到我的 phone,我打算从那里接收它。
我从他们的官方网站上尝试了以下类似“hello world”的程序,但我无法让它在一点之后继续。
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Ugh, the world? Well.. hello, I guess")
application = tornado.web.Application([
(r"/", MainHandler),
])
application.listen(8881)
tornado.ioloop.IOLoop.instance().start()
# I cannot get this line to execute!!
print("Hi!!")
经验:Python 基础,Arduino C++ 中级,none networking/web
这是因为 Tornado 的 IOLoop.start()
方法是一个“阻塞”调用,这意味着它不会 return 直到满足某些条件。这就是为什么您的代码“卡在”该行的原因。 documentation for this method 状态:
Starts the I/O loop.
The loop will run until one of the callbacks calls stop(), which will
make the loop stop after the current event iteration completes.
通常,对 IOLoop.start()
的调用将是程序中的最后一件事。如果您想停止 Tornado 应用程序然后继续执行其他操作,则例外。
这里有两种可能的解决方案,具体取决于您要实现的目标:
- 从处理程序调用
self.stop()
。这将停止 Tornado 应用程序,IOLoop.start()
将 return,然后您的打印将执行。
- 从您的处理程序中调用
print("Hi!!")
。
您试图在启动事件循环后打印到 STDOUT,因此打印语句永远不会出现。基本上,您要在端口 8881 上创建一个不断侦听请求的 HTTP 服务器。无论您希望服务器执行什么逻辑,都需要在回调中,例如 MainHandler
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Ugh, the world? Well.. hello, I guess")
# All your SMS logic potentially goes here
self.write("Sent SMS to xyz...")
application = tornado.web.Application(
[
(r"/", MainHandler),
]
)
application.listen(8881)
tornado.ioloop.IOLoop.instance().start()
然后通过 HTTP 调用触发端点
curl <IP ADDRESS OF YOUR PI>:8881
我正在尝试通过本地网络 Raspberry Pi 将纯文本发送到我的 phone,我打算从那里接收它。
我从他们的官方网站上尝试了以下类似“hello world”的程序,但我无法让它在一点之后继续。
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Ugh, the world? Well.. hello, I guess")
application = tornado.web.Application([
(r"/", MainHandler),
])
application.listen(8881)
tornado.ioloop.IOLoop.instance().start()
# I cannot get this line to execute!!
print("Hi!!")
经验:Python 基础,Arduino C++ 中级,none networking/web
这是因为 Tornado 的 IOLoop.start()
方法是一个“阻塞”调用,这意味着它不会 return 直到满足某些条件。这就是为什么您的代码“卡在”该行的原因。 documentation for this method 状态:
Starts the I/O loop.
The loop will run until one of the callbacks calls stop(), which will make the loop stop after the current event iteration completes.
通常,对 IOLoop.start()
的调用将是程序中的最后一件事。如果您想停止 Tornado 应用程序然后继续执行其他操作,则例外。
这里有两种可能的解决方案,具体取决于您要实现的目标:
- 从处理程序调用
self.stop()
。这将停止 Tornado 应用程序,IOLoop.start()
将 return,然后您的打印将执行。 - 从您的处理程序中调用
print("Hi!!")
。
您试图在启动事件循环后打印到 STDOUT,因此打印语句永远不会出现。基本上,您要在端口 8881 上创建一个不断侦听请求的 HTTP 服务器。无论您希望服务器执行什么逻辑,都需要在回调中,例如 MainHandler
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Ugh, the world? Well.. hello, I guess")
# All your SMS logic potentially goes here
self.write("Sent SMS to xyz...")
application = tornado.web.Application(
[
(r"/", MainHandler),
]
)
application.listen(8881)
tornado.ioloop.IOLoop.instance().start()
然后通过 HTTP 调用触发端点
curl <IP ADDRESS OF YOUR PI>:8881