【问题标题】:Does twisted, cyclone or tornado do SMP multicore out of the boxTwisted、Cyclone 或 Tornado 是否开箱即用 SMP 多核
【发布时间】:2014-01-25 21:47:19
【问题描述】:

我想在具有 8 个内核的 AWS Linux 服务器上使用上述 3 个非阻塞服务器中的任何一个。在任何文档中都不清楚 SMP 是否在相应的 helloworld 或任何其他示例中实现。

例如,这个旋风 helloworld 没有提及内核或 SMP 或每个内核的线程数。

import cyclone.web

class MainHandler(cyclone.web.RequestHandler):
    def get(self):
        self.write("Hello, world")


class Application(cyclone.web.Application):
    def __init__(self):
        cyclone.web.Application.__init__(self, [(r"/", MainHandler)],
                                         xheaders=False)

或者这个扭曲的:

from twisted.web import server, resource
from twisted.internet import reactor
class HelloResource(resource.Resource):
    isLeaf = True
    numberRequests = 0

    def render_GET(self, request):
        self.numberRequests += 1
        request.setHeader("content-type", "text/plain")
        return "I am request #" + str(self.numberRequests) + "\n"

reactor.listenTCP(8080, server.Site(HelloResource()))
reactor.run()

或者龙卷风……

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

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

事实上,很难确定它们是否是非阻塞的。

【问题讨论】:

标签: python twisted tornado twisted.web cyclone


【解决方案1】:

Tornado 的 HTTPServer 支持多进程模式,使用 bind(port)start(num_procs) 方法。
http://www.tornadoweb.org/en/stable/tcpserver.html#tornado.tcpserver.TCPServer.start

【讨论】:

  • 自动。 “如果 num_processes 为 None 或
  • 是的,但请注意,num_processes 的默认值为 1。应用程序必须选择加入多进程模式。
  • 该死的。刚刚注意到小字。谢谢
  • (1) 可以是不同的线程而不是进程,这样我们可以节省内存吗? (2) 我们如何利用 X cores 并指定每个 websocket 将由一个 core 处理?
  • 请不要在 cmets 中对一个非常老的问题提出新问题。将您的问题作为新问题提出。
【解决方案2】:

CPython 进程使用全局解释器锁,因此如果只运行一个 Python 进程,则无法真正利用硬件中可用的多个线程。

【讨论】:

  • 这就是为什么有些(如果不是全部)fork 进程要么隐式地添加,要么必须显式地添加它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-12
  • 2011-03-28
  • 2014-06-11
  • 2019-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多