twisted

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

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 14:18:25
诗歌下载服务器 到目前为止,我们已经学习了大量关于诗歌下载客户端的 Twisted 的知识,接下来,我们使用 Twisted 重新实现我们的服务器端。利益于 Twisted 的抽象机制,接下来你会发现我们前面已经几乎全部学习到这部分知识了。其实现源码在 twisted-server-1/fastpoetry.py 中 。之所以称其为 fastpoetry 是因为其并没有任何延迟的传输诗歌。注意到,其代码量比客户端少多了。 让我们一部分一部分地来看服务端的实现,首先是 poetryProtocol : class PoetryProtocol(Protocol): def connectionMade(self): self.transport.write(self.factory.poem) self.transport.loseConnection() 如同客户端的实现,服务器端使用 Protocol 来管理连接(在这里,连接是由客户端发起的)。这里的 Protocol 实现了我们的诗歌下载逻辑的服务器端。由于我们协议逻辑处理的是单向的,服务器端的 Protocol 只负责发送数据。如果你访问服务器端,协议请求服务器在连接建立后立即发送诗歌,因此我实现了 connectionMade 方法,其会在 Protocol 中创建一个连接时被激活执行。 这个方法告诉 Transport

(转) Twisted :第十九部分 改变之前的想法

流过昼夜 提交于 2019-11-28 14:18:11
简介 Twisted是一个正在进展的项目,它的开发者会定期添加新的特性并且扩展旧的特性. 随着Twisted 10.1.0发布,开发者向 Deferred 类添加了一个新的特性—— cancellation ——这正是我们今天要研究的. 异步编程将请求和响应解耦了,如此又带来一个新的可能性:在请求结果和返回结果之间,你可能决定不再需要这个结果了.考虑一下 :doc:`p14` 中的诗歌代理服务器.下面是这个如何工作的,至少对于诗歌的第一次请求: 一个对诗歌的请求来了. 这个代理联系实际服务器以得到这首诗 一旦这首诗完成,将其发送给原发出请求的代理 看起来非常完美,但是如果客户端在获得诗歌之前挂了怎么办?也许它们先前请求 Paradise Lost 的全部内容,随后它们决定实际想要的是 Kojo 的俳句.我们的代理将陷入下载前者,并且那个慢服务器会等好一会.最好的策略便是关闭连接,让慢服务器回去顺觉. 回忆一下 第十五 部分,展示了同步程序控制流的概念.在那张图中我们可以看到函数调用自上而下,异常是自下而上.如果我们希望取消一个同步调用(这仅是假设),控制流的传递方向与函数调用的方向一致,都是从高层传向底层,如图38所示: 图38:同步程序流,含假想取消操作 当然,在同步程序中这是不可能的,因为高层的代码在底层操作结束前没有恢复运行,自然也就没有什么可取消的.但是在异步程序中

What's so cool about Twisted? [closed]

旧街凉风 提交于 2019-11-28 13:38:44
问题 I'm increasingly hearing that Python's Twisted framework rocks and other frameworks pale in comparison. Can anybody shed some light on this and possibly compare Twisted with other network programming frameworks. 回答1: There are a lot of different aspects of Twisted that you might find cool. Twisted includes lots and lots of protocol implementations, meaning that more likely than not there will be an API you can use to talk to some remote system (either client or server in most cases) - be it

python异步库twisted

£可爱£侵袭症+ 提交于 2019-11-28 13:37:21
何为异步 其实我们谈论的异步库都是基于计算机模型Event Loop 举例  我们知道,每一个程序运行都会开启一个进程,在tcpserver服务器历史上,主要有3种方式来处理客户端来的连接。   为了方便说明,我们把tcpserver想象成对银行办理业务的过程,你每次去银行办理业务的时候,其实真正办理业务的时间并不长,其中很多时候,银行的工作人员也在等待,比如她操作一笔业务,电脑还没有及时反应过来,她没事可做,只能等待;打印各种文件的时候,也在等待。这其实跟我们的tcpserver是一样的,很多应用,我们的tcpserver一直在等待。   第一,阻塞排队。银行只开通一个窗口,每个人过来,都要排队,每一个人都要等待,其中还有很多时候,银行的工作人员在等电脑、打印机的操作时间。这种方式效率最低下。   第二,子进程。每次来一个客户,银行都开启一个窗口,专门接待,但银行的窗口不是无限的,每开启一个窗口,都有代价。这种方式比上面好了一些,但效率还不是那么高。   第三,线程。银行看到每个业务员虽然一直在忙活,但中间等待时间过长,效率提高不上来。于是,领导规定,每个业务员同时处理10个客户(1个进程开始10个线程),在处理客户1的空余时间,再处理客户2,或者其他的。嗯,貌似效率提高了,但业务员同时接这么多客户,极其容易出错(线程模式,确实容易出错,而且还不好控制,通常线程都只是处理比较单一

When to use Tornado, when to use Twisted / Cyclone / GEvent / other [closed]

流过昼夜 提交于 2019-11-28 13:07:08
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . Which of these frameworks / libraries would be the best choise for building modern multiuser web application? I would love to have an asynchronous webserver which will allow me to scale easly. What solution will give the best performance / scalability / most useful framework (in

Re-using deferred objects in Twisted

隐身守侯 提交于 2019-11-28 11:54:19
问题 In Twisted, it seems that a deferred object can only be used once after its callback has fired, as opposed to other "promise"-based libraries I've worked with: from twisted.internet import defer class Foo(object): def __init__(self): self.dfd = defer.Deferred() def async(self): return self.dfd @defer.inlineCallbacks def func(self): print 'Started!' result = yield self.async() print 'Stopped with result: {0}'.format(result) if __name__ == '__main__': foo = Foo() foo.func() import time time

Sending data from one Protocol to another Protocol in Twisted?

ⅰ亾dé卋堺 提交于 2019-11-28 11:45:38
One of my protocols is connected to a server, and with the output of that I'd like to send it to the other protocol. I need to access the 'msg' method in ClassA from ClassB but I keep getting: exceptions.AttributeError: 'NoneType' object has no attribute 'write' Actual code: from twisted.words.protocols import irc from twisted.internet import protocol from twisted.internet.protocol import Protocol, ClientFactory from twisted.internet import reactor IRC_USERNAME = 'xxx' IRC_CHANNEL = '#xxx' T_USERNAME = 'xxx' T_PASSWORD = md5.new('xxx').hexdigest() class ircBot(irc.IRCClient): def _get_nickname

ReactorNotRestartable - Twisted and scrapy

☆樱花仙子☆ 提交于 2019-11-28 09:34:30
Before you link me to other answers related to this, note that I've read them and am still a bit confused. Alrighty, here we go. So I am creating a webapp in Django. I am importing the newest scrapy library to crawl a website. I am not using celery (I know very little about it, but saw it in other topics related to this). One of the url's of our website, /crawl/, is meant to start the crawler running. It's the only url in our site that requires scrapy to be used. Here is the function which is called when the url is visited: def crawl(request): configure_logging({'LOG_FORMAT': '%(levelname)s: %

Building a RESTful Flask API for Scrapy

感情迁移 提交于 2019-11-28 09:28:15
The API should allow arbitrary HTTP get requests containing URLs the user wants scraped, and then Flask should return the results of the scrape. The following code works for the first http request, but after twisted reactor stops, it won't restart. I may not even be going about this the right way, but I just want to put a RESTful scrapy API up on Heroku, and what I have so far is all I can think of. Is there a better way to architect this solution? Or how can I allow scrape_it to return without stopping twisted reactor (which can't be started again)? from flask import Flask import os import

Get Scrapy crawler output/results in script file function

时光毁灭记忆、已成空白 提交于 2019-11-28 07:57:40
问题 I am using a script file to run a spider within scrapy project and spider is logging the crawler output/results. But i want to use spider output/results in that script file in some function .I did not want to save output/results in any file or DB. Here is Script code get from https://doc.scrapy.org/en/latest/topics/practices.html#run-from-script from twisted.internet import reactor from scrapy.crawler import CrawlerRunner from scrapy.utils.log import configure_logging from scrapy.utils