twisted

The latest recommendation for Comet in Python? [closed]

百般思念 提交于 2019-11-27 11:08:42
I'm going to be implementing Comet in Python (even though I hear good things about erlycomet I'm not thrilled about supporting an Erlang-based web server in addition to everything else in our back end). I've found several possibilities: Diesel Tornado Twisted-comet Orbited (uses twisted) (there are also some other choices that interface with Java servers, but I'm not interested in those) Can somebody make a recommendation among these implementations, considering performance, community, and ease of implementation? I am personally using Orbited, both because I am already using Twisted and

Queue remote calls to a Python Twisted perspective broker?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 10:12:34
问题 The strength of Twisted (for python) is its asynchronous framework (I think). I've written an image processing server that takes requests via Perspective Broker. It works great as long as I feed it less than a couple hundred images at a time. However, sometimes it gets spiked with hundreds of images at virtually the same time. Because it tries to process them all concurrently the server crashes. As a solution I'd like to queue up the remote_calls on the server so that it only processes ~100

A clean, lightweight alternative to Python's twisted? [closed]

筅森魡賤 提交于 2019-11-27 09:55:35
A (long) while ago I wrote a web-spider that I multithreaded to enable concurrent requests to occur at the same time. That was in my Python youth, in the days before I knew about the GIL and the associated woes it creates for multithreaded code (IE, most of the time stuff just ends up serialized!)... I'd like to rework this code to make it more robust and perform better. There are basically two ways I could do this: I could use the new multiprocessing module in 2.6+ or I could go for a reactor / event-based model of some sort. I would rather do the later since it's far simpler and less error

(转) Twisted : 第九部分 第二个小插曲,Deferred

邮差的信 提交于 2019-11-27 09:26:15
更多关于回调的知识 稍微停下来再思考一下回调的机制。尽管对于以 Twisted 方式使用 Deferred 写一个简单的异步程序已经非常了解了,但 Deferred 提供更多的是只有在比较复杂环境下才会用到的功能。因此,下面我们自己想出一些复杂的环境,以此来观察当使用回调编程时会遇到哪些问题。然后,再来看看 deferred 是如何解决这些问题的。 因此,我们为诗歌下载客户端添加了一个假想的功能。设想一些计算机科学家发明了一种新诗歌关联算法, Byronification 引擎 。这个漂亮的算法根据一首诗歌生成一首使用 Lord Byro n 式的同样的诗歌。另外,专家们提供了其 Python 的接口,即: class IByronificationEngine(Interface): def byronificate(poem): """ Return a new poem like the original, but in the style of Lord Byron. Raises GibberishError if the input is not a genuine poem. """ 像大多数高尖端的软件一样,其实现都存在着许多 bugs 。这意外着除了已知的异常外,这个 byronificate 方法可能会抛出一些专家当时没有预料到的异常出来。

ReactorNotRestartable error in while loop with scrapy

痴心易碎 提交于 2019-11-27 09:26:05
I get twisted.internet.error.ReactorNotRestartable error when I execute following code: from time import sleep from scrapy import signals from scrapy.crawler import CrawlerProcess from scrapy.utils.project import get_project_settings from scrapy.xlib.pydispatch import dispatcher result = None def set_result(item): result = item while True: process = CrawlerProcess(get_project_settings()) dispatcher.connect(set_result, signals.item_scraped) process.crawl('my_spider') process.start() if result: break sleep(3) For the first time it works, then I get error. I create process variable each time, so

(转) Twisted :第七部分 小插曲,Deferred

对着背影说爱祢 提交于 2019-11-27 09:25:50
回调函数的后序发展 在第六部分我们认识这样一个情况: 回调是 Twisted 异步编程中的基础。除了与 reactor 交互外,回调可以安插在任何我们写的 Twisted 结构内。因此在使用 Twisted 或其它基于 reactor 的异步编程体系时,都意味需要将我们的代码组织成一系列由 reactor 循环可以激活的回调函数链。 即使一个简单的 get_poetry 函数都需要回调,两个回调函数中一个用于处理正常结果而另一个用于处理错误。作为一个 Twisted 程序员,我们必须充分利用这一点。应该花点时间思考一下如何更好地使用回调及使用过程中会遇到什么困难。 分析下 3.1 版本中的 get_poetry 函数: ... def got_poem(poem): print poem reactor.stop() def poem_failed(err): print >>sys.stderr, 'poem download failed' print >>sys.stderr, 'I am terribly sorry' print >>sys.stderr, 'try again later?' reactor.stop() get_poetry(host, port, got_poem, poem_failed) reactor.run() 我们想法很简单: 1.

(转) Twisted : 第十二部分 改进诗歌下载服务器

a 夏天 提交于 2019-11-27 09:25:38
新的服务器实现 这里我们要新写一个 Twisted 版的服务器。然后,再来讨论一些 Deferred 的新功能。 在第九、十部分,我们提出了诗歌转换引擎这个概念。由于其实现太过简单,因此我们用随机选择来模拟了可能会出现转换失败的情景。但如果转换引擎位于服务器端,那么当服务器宕机就会出现真实的转换失败的情景了。 因此,在这部分我们要实现一个诗歌样式转换服务器,然后在一个部分,我们会重写我们的诗歌下载客户端来使用这一服务并且学习 Deferred 的新功能。 设计协议 到目前为止,服务器端与客户端之间的交互都是单向的。但样式转换服务需要两者进行双向交互 - 客户端将原始式样的诗歌发送给乳服务器,然后服务器将转换格式并将其发送给对应的客户端。因此,我们需要使用、或自己实现一个协议来实现这种交互。 我们设计服务器端可以提供若干种转换服务,而让客户端来进行选择。因此客户端需要向服务器端发送两部分信息:转换方式名与诗歌原始内容。服务器只是将转换格式之后的诗歌发送给客户端。这里使用到了简单的 运程调用 。 Twisted 支持需要种协议来解决这个问题: XML-RPC,Perspective Broker,AMP 。 但介绍使用其中任何一种都需要大量的时间,因此我们使用自己实现的协议。我们约定客户端发送内容格式如下: 转换名称 . 诗歌内容 我们将其以 netstring 格式编码

(转) Twisted : 第十四部分 Deferred用于同步环境

给你一囗甜甜゛ 提交于 2019-11-27 09:23:55
介绍 这部分我们要介绍 Deferred 的另外一个功能。便于讨论,我们设定如下情景:假设由于众多的内部网请求一个外部诗歌下载服务器,但由于这个外部下载服务器性能太差或请求负荷太重。因此,我们不想将所有的内部请求全部发送到外部服务器。我们的处理办法是,在中间添加一个缓存代理。当一个请求来到后,我们可以从中间缓存代理中找到缓存的备份(如果有缓存)或者直接从外部服务器获得。部署图如图 30 所示: 图 30 缓存代理服务器 考虑到,客户端端发送请求来时,此缓存代理可能会将本地的缓冲的诗歌取出并回复,也有可能需要异步等待外部诗歌下载服务器的诗歌回复。如此一来,就会出现这样的情景:客户端发送来的请求,缓存代理处理请求可能是同步也可能是异步。 要解决这个需要,就用到了 Deferred 的另一个特性,即可以在将 Deferred 返回前就激活这个 Deferred 。之所以可以这样做,是因为你可以在一个已经激活的 deferred 上添加回调处理函数。一个非常值得注意的是:已经被激活的 deferred 可以立即激活新添加的回调处理函数。图 31 表示一个已经激活的 deferred : 图 31 已经激活的 deferred 如果在此时,我们再为其另一对 callback/errback ,那么会立即激活新的回调并执行。如图 32 图 32 同一个 deferred 在添加新的回调之后

Caveats of select/poll vs. epoll reactors in Twisted

吃可爱长大的小学妹 提交于 2019-11-27 08:56:33
问题 Everything I've read and experienced ( Tornado based apps ) leads me to believe that ePoll is a natural replacement for Select and Poll based networking, especially with Twisted. Which makes me paranoid, its pretty rare for a better technique or methodology not to come with a price. Reading a couple dozen comparisons between epoll and alternatives shows that epoll is clearly the champion for speed and scalability, specifically that it scales in a linear fashion which is fantastic. That said,

Intermittent “getrandom() initialization failed” using scrapy spider

青春壹個敷衍的年華 提交于 2019-11-27 07:53:51
问题 I built a scrapy spider (scrapy 1.4). This spider is triggered on demand from a django website through django-rq and supervisord. Here is the supervisord job that is listening for django-rq events (reddit is used as broker) [program:rq_worker] command=python3 manage.py rqworker default directory=/var/www/django-app autostart=true autorestart=true stderr_logfile=/var/log/rq_worker.err.log stdout_logfile=/var/log/rq_worker.out.log This set up is running fine. However, from time to time (I