pygame's API for initializing a display has a couple attractive sounding flags. Or at least, they sound attractive once you notice that updating your 320x240 window at 30fps is consuming all available cycles on your brand new Intel i9 CPU. They're HWSURFACE
and DOUBLEBUF
. Hardware surfaces and double buffering is how you make graphics fast, right?
Well... no. You probably can't get a hardware surface anyway, and double buffering is unlikely to help until you figure out how to have a window larger than 320x240 anyway.
What you really need to do to get reasonable rendering performance is make sure that any images you blit to the display have the same color depth. You can do this with the Surface.convert
method (you get back a Surface when you load an image from a file, eg a png or a gif). Check the depth of your display surface and then convert your image surfaces to that depth (and just keep the converted versions). Blitting them will get hundreds of times faster.
It's a pretty simple thing, but it's easy to get distracted by HWSURFACE
and not notice the depth mismatch (like I did for three days).
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND exarkun 5621 8.9 12.1 275416 251248 ? Rl 2009 70025:19 /usr/bin/python /usr/bin/twistd -f infobot.tap
JSONEncoder.iterencode
method, I thought it would be great used in combination with cooperate to create a producer. This would let an application serialize a large structure to JSON without multiple processes, threads, or unreasonably blocking the reactor.from json import JSONEncoder from twisted.internet.task import cooperate class AsyncJSON(object): def __init__(self, value): self._value = value def beginProducing(self, consumer): self._consumer = consumer self._iterable = JSONEncoder().iterencode(self._value) self._consumer.registerProducer(self, True) self._task = cooperate(self._produce()) d = self._task.whenDone() d.addBoth(self._unregister) return d def pauseProducing(self): self._task.pause() def resumeProducing(self): self._task.resume() def stopProducing(self): self._task.stop() def _produce(self): for chunk in self._iterable: self._consumer.write(chunk) yield None def _unregister(self, passthrough): self._consumer.unregisterProducer() return passthrough
iterencode
method, this avoids spending too much time generating json output at once. Instead, a little bit of the input will be serialized at a time, and each short resulting string is available from the iterator returned by iterencode
.cooperate
, the _produce
generator will iterated in a way that lets it cooperate with the reactor and other event sources/handlers. A few chunks of json data will be written to the consumer, then execution will switch away to something else, then come back and a few more will be written, and so on.AsyncJSON
in a resource:from twisted.web.resource import Resource from twisted.web.server import NOT_DONE_YET class BigIntegerList(Resource): def render_GET(self, request): length = int(request.args['length'][0]) d = AsyncJSON(range(length)).beginProducing(request) d.addCallback(lambda ignored: request.finish()) return NOT_DONE_YET
epa是什么营养物质 | 一什么鼓 | 月经推迟量少是什么原因 | 最高法院院长什么级别 | 出汗发粘是什么原因 |
老人脚背肿是什么原因 | 什么大专好就业 | 舌中间有裂纹是什么原因 | 净土是什么意思 | 2.3是什么星座 |
反复发烧是什么原因 | 什么是全日制本科 | 手比脸白是什么原因 | 三叉神经痛吃什么药 | 女人肚子大是什么原因 |
握手言和是什么意思 | 肌肉纤维化是什么意思 | 心寒是什么意思 | 舌苔又白又厚是什么原因 | 比目鱼长什么样 |
心颤吃什么药效果好hcv9jop8ns1r.cn | 月下老人什么意思hcv9jop4ns2r.cn | 什么是脱敏520myf.com | 反复感冒是什么原因引起的hcv9jop6ns8r.cn | 瓜皮是什么意思hcv8jop6ns3r.cn |
吃小米粥有什么好处和坏处hcv8jop9ns5r.cn | 喝酒眼睛红是什么原因hcv9jop7ns1r.cn | 苦海翻起爱恨是什么歌hcv8jop2ns7r.cn | 血小板低吃什么补的快hcv8jop4ns4r.cn | 反流性胃炎吃什么药96micro.com |
PA医学上是什么意思hcv8jop1ns5r.cn | 热惊厥病发是什么症状hcv8jop1ns2r.cn | 冠脉钙化是什么意思hcv7jop5ns2r.cn | 什么日子适合搬家hcv8jop4ns1r.cn | 梦见火是什么预兆hcv9jop6ns5r.cn |
肛门不舒服是什么原因hcv8jop5ns8r.cn | 高血压什么症状hcv8jop0ns2r.cn | 阿司匹林主治什么病hcv8jop0ns7r.cn | 姐妹是什么生肖hcv7jop9ns5r.cn | 肌层回声不均匀是什么意思hcv8jop5ns3r.cn |