Python 为什么龙卷风不会';你不能同时去吗?

Python 为什么龙卷风不会';你不能同时去吗?,python,asynchronous,tornado,Python,Asynchronous,Tornado,下面是我的测试代码。我使用的是Python2.7,其futures安装使用: pip install futures 以下是我的演示代码: import tornado.ioloop import tornado.web from tornado.gen import coroutine, Task from tornado.concurrent import Future import time class MainHandler(tornado.web.RequestHandler):

下面是我的测试代码。我使用的是Python2.7,其
futures
安装使用:

pip install futures
以下是我的演示代码:

import tornado.ioloop
import tornado.web
from tornado.gen import coroutine, Task
from tornado.concurrent import Future
import time


class MainHandler(tornado.web.RequestHandler):
    @coroutine
    def get(self):
        print "in"
        res = yield Task(self._work)
        self.write(res)

    def _work(self, callback):
        time.sleep(10)
        callback("hello world!")

if __name__ == "__main__":
    application = tornado.web.Application([
        (r"/", MainHandler),
    ])
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
这段代码应该是并发的,不是吗?然而,事实并非如此

我用Firefox和IE进行了测试。我想我犯了一些错误。你能指出我的错误就好了

一次只有一个请求(
http://localhost:8888/

import tornado.ioloop
import tornado.web
from tornado.gen import coroutine, Return, Task
from tornado.process import Subprocess
from tornado.concurrent import Future
from threading import Thread
import time

@coroutine
def async_sleep(timeout):
    """ Sleep without blocking the IOLoop. """
    yield Task(tornado.ioloop.IOLoop.instance().add_timeout, time.time() + timeout)

class MainHandler(tornado.web.RequestHandler):
    @coroutine
    def get(self):
        print "in"
        res = yield self._work()
        self.write(res)

    @coroutine
    def _work(self):
        yield async_sleep(5)
        raise Return("hello world!")

if __name__ == "__main__":
    application = tornado.web.Application([
        (r"/", MainHandler),
    ])
    application.listen(8888)
    ioloop=tornado.ioloop.IOLoop.instance()
    Thread(target=ioloop.start).start()

由于您在注释中表示希望通过tornado运行子流程,下面是一个示例,说明如何执行此操作。我还修复了一个逻辑错误,即您在调用
\u work
时创建
任务
,该错误将无法按您预期的方式运行:

import tornado.ioloop
import tornado.web
from tornado.gen import coroutine, Return
from tornado.process import Subprocess
from tornado.concurrent import Future

class MainHandler(tornado.web.RequestHandler):
    @coroutine
    def get(self):
        print "in"
        res = yield self._work()
        self.write(res)

    @coroutine
    def _work(self):
        p = Subprocess(['sleep', '10'])
        f = Future()
        p.set_exit_callback(f.set_result)
        yield f
        raise Return("hello world!")

if __name__ == "__main__":
    application = tornado.web.Application([
        (r"/", MainHandler),
    ])
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

如您所见,我创建了
\u work
协同程序,然后使用类执行命令。我创建了
Future
对象,并指示
子流程调用
Future。设置\u结果(返回\u Subprocess的\u code\u)
当它完成时。然后我在
Future
实例上调用了
yield
。这会使代码等待子流程完成,而不会阻塞I/O循环。

代码中的
时间。sleep
是一种阻塞方法。

您需要
tornado.gen.sleep
(一种非阻塞方法,在tornado 4.1中新增)这里改为。

你不能使用时间。睡眠
,因为这将阻止tornado的I/O循环。你需要使用非阻塞方法来睡眠。请参阅复制问题以获取示例。@dano
sleep
的可能重复我认为这是真正的问题。实际上,我将调用bash命令,它可能需要这样的时间ime周期。有什么好的建议吗?看起来不错。但我注意到一个问题。我启动了两个请求,在第一个请求返回之前,第二个请求无法处理。你能告诉我一种方法使处理程序解锁吗?为了方便起见,我引用dup并使用
async\u sleep
进行模拟。请查看更新。@hustmphrr正在测试吗这是通过web浏览器实现的?如果是这样,您可能遇到了我描述的问题。基本上,您看到了浏览器的限制。如果您同时使用两个浏览器或通过curl进行测试,它会很好地工作。我想知道该命令是否不是
['sleep','10']
,而是
['ls','l']
,如何将
ls-l
的结果返回到
get(self)