twisted

Python twisted: how to schedule?

痞子三分冷 提交于 2019-12-03 03:28:43
Having 1-day experience in Twisted I try to schedule message sending in reply to tcp client: import os, sys, time from twisted.internet import protocol, reactor self.scenario = [(1, "Message after 1 sec!"), (4, "This after 4 secs"), (2, "End final after 2 secs")] for timeout, data in self.scenario: reactor.callLater(timeout, self.sendata, data) print "waited %d time, sent %s\n"%(timeout, data) Now it sends messages, but I have 2 problems: 1) "timeout" is going from "now", and I want to count it after each previous task was completed (previous message was sent) 2) I don't know how to close

How do you *properly* query Redis from Tornado?

a 夏天 提交于 2019-12-03 03:14:05
问题 I'm curious what the recommended method of querying Redis (or any DB for that matter) is from Tornado. I've seen some examples like https://gist.github.com/357306 but they all appear to be using blocking calls to redis. My understanding is that to avoid grinding Tornado to a halt, I need to be using non-blocking DB libraries like the ones developed for Twisted. Am I wrong? How is this supposed to be done? 回答1: When it comes to blocking commands like BLPOP or listening to a Pub/Sub channel you

twisted: difference between `defer.execute` and `threads.deferToThread`

匿名 (未验证) 提交于 2019-12-03 03:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: What is the difference between defer.execute() and threads.deferToThread() in twisted? Both take the same arguments - a function, and parameters to call it with - and return a deferred which will be fired with the result of calling the function. The threads version explicitly states that it will be run in a thread. However, if the defer version doesn't, then what would ever be the point of calling it? Code that runs in the reactor should never block, so any function it calls would have to not block. At that point, you could just do defer

Need help writing a twisted proxy

拈花ヽ惹草 提交于 2019-12-03 03:05:01
I want to write a simple proxy that shuffles the text in the body of the requested pages. I have read parts of the twisted documentation and some other similar questions here on stackoverflow but I'm a bit of a noob so I still don't get it. This is what I have now, I don't know how to access and modify the page from twisted.web import proxy, http from twisted.internet import protocol, reactor from twisted.python import log import sys log.startLogging(sys.stdout) class ProxyProtocol(http.HTTPChannel): requestFactory = PageHandler class ProxyFactory(http.HTTPFactory): protocol = ProxyProtocol if

Threads in twisted… how to use them properly?

末鹿安然 提交于 2019-12-03 03:02:00
I need to write a simple app that runs two threads: - thread 1: runs at timed periods, let's say every 1 minute - thread 2: just a 'normal' while True loop that does 'stuff' if not the requirement to run at timed interval I would have not looked at twisted at all, but simple sleep(60) is not good enough and construction like: l = task.LoopingCall(timed_thread) l.start(60.0) reactor.run() Looked really simple to achieve what I wanted there. Now, how do I 'properly' add another thread? I see two options here: Use threading library and run two 'python threads' one executing my while loop, and

Stopping Twisted from swallowing exceptions

匿名 (未验证) 提交于 2019-12-03 02:46:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there a way to stop Twisted reactor from automatically swallowing exceptions (eg. NameError)? I just want it to stop execution, and give me a stack trace in console? There's even a FAQ question about it, but to say the least, it's not very helpful. Currently, in every errback I do this: def errback(value): import traceback trace = traceback.format_exc() # rest of the errback... but that feels clunky, and there has to be a better way? Update In response to Jean-Paul's answer, I've tried running the following code (with Twisted 11.1 and 12

How do I add two integers together with Twisted?

烂漫一生 提交于 2019-12-03 02:27:25
问题 I have two integers in my program; let's call them " a " and " b ". I would like to add them together and get another integer as a result. These are regular Python int objects. I'm wondering; how do I add them together with Twisted? Is there a special performAsynchronousAddition function somewhere? Do I need a Deferred ? What about the reactor? Is the reactor involved? 回答1: OK, to be clear. Twisted doesn't do anything about cpu bound tasks and for good reason. there's no way to make a compute

ImportError: No module named twisted.internet

匿名 (未验证) 提交于 2019-12-03 02:18:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I installed python 2.7.5 which is working fine. I then install scrapy (which, I think, uses twisted internally). My scrapy spider is also working fine. I installed twisted: sudo apt-get install python-twisted Then, I created a sample program using Echo Server code shown here Here is the code from twisted.internet import protocol, reactor class Echo(protocol.Protocol): def dataReceived(self, data): self.transport.write(data) class EchoFactory(protocol.Factory): def buildProtocol(self, addr): return Echo() reactor.listenTCP(1234, EchoFactory()

Can't Start Carbon - 12.04 - Python Error - ImportError: cannot import name daemonize

匿名 (未验证) 提交于 2019-12-03 02:16:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am really hoping someone can help me as I have spent at-least 15 hours trying to fix this problem. I have been given a task by a potential employer and my solution is to use graphite/carbon/collectd. I am trying to run and install carbon / graphite 0.9.12 but I simply can't get carbon to start. Every time I try and start carbon I end up with the following error. I am using a bash script to install to keep everything consistent. I don't really know python at all so would appreciate any help you can provide. /etc/rc0.d/K20carbon-cache -> ..

twisted http client

妖精的绣舞 提交于 2019-12-03 02:10:40
I am after an example describing the usage of Twisted's HTTP Client. After reading the excellent blog post on the internals of Twisted , I understand how the "Factory" and "Protocol" components play their role but I am unclear on how to introduce "Request" in the overall Client flow. More specifically, I need to be able to perform HTTP GET and POST requests to a remote server using Twisted. Updated : after a discussion on irc #twisted / #python, it seems that twisted.web2 is fading away in favor of beefing up functionality on twisted.web e.g. Agent. As of Twisted 9.0, there are actually two