我会用 Stackless Python 做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2220645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What would I use Stackless Python for?
提问by zaharpopov
There are many questions related to Stackless Python. But none answering this my question, I think (correct me if wrong - please!). There's some buzz about it all the time so I curious to know. What would I use Stackless for? How is it better than CPython?
有很多关于 Stackless Python 的问题。但是没有人回答我的问题,我想(如果错了请纠正我 - 请!)。一直有一些关于它的嗡嗡声,所以我很想知道。我会用 Stackless 做什么?它比 CPython 好在哪里?
Yes it has green threads (stackless) that allow quickly create many lightweight threads as long as no operations are blocking (something like Ruby's threads?). What is this great for? What other features it has I want to use over CPython?
是的,它有绿色线程(无堆栈),只要没有操作阻塞(类似于 Ruby 的线程?),它就可以快速创建许多轻量级线程。这有什么用?我想通过 CPython 使用它还有哪些其他功能?
采纳答案by Meh
It allows you to work with massive amounts of concurrency. Nobody sane would create one hundred thousand system threads, but you can do this using stackless.
它允许您处理大量并发。没有理智的人会创建 10 万个系统线程,但您可以使用无堆栈来做到这一点。
This article tests doing just that, creating one hundred thousand tasklets in both Python and Google Go (a new programming language): http://dalkescientific.com/writings/diary/archive/2009/11/15/100000_tasklets.html
本文测试这样做,在 Python 和 Google Go(一种新的编程语言)中创建十万个小任务:http: //dalkescientific.com/writings/diary/archive/2009/11/15/100000_tasklets.html
Surprisingly, even if Google Go is compiled to native code, and they tout their co-routines implementation, Python still wins.
令人惊讶的是,即使 Google Go 被编译为本机代码,并且他们吹捧他们的协程实现,Python 仍然获胜。
Stackless would be good for implementing a map/reduce algorithm, where you can have a very large number of reducers depending on your input data.
Stackless 非常适合实现 map/reduce 算法,在这种算法中,您可以根据输入数据拥有大量的 reducer。
回答by Kylotan
Stackless Python's main benefit is the support for very lightweight coroutines. CPython doesn't support coroutines natively (although I expect someone to post a generator-based hack in the comments) so Stackless is a clear improvement on CPython when you have a problem that benefits from coroutines.
Stackless Python 的主要好处是支持非常轻量级的协程。CPython 本身不支持协程(尽管我希望有人在评论中发布基于生成器的 hack),因此当您遇到可从协程中受益的问题时,Stackless 是对 CPython 的明显改进。
I think the main area where they excel are when you have many concurrent tasks running within your program. Examples might be game entities that run a looping script for their AI, or a web server that is servicing many clients with pages that are slow to create.
我认为他们擅长的主要领域是当您的程序中运行许多并发任务时。示例可能是为其 AI 运行循环脚本的游戏实体,或者为许多客户端提供服务的 Web 服务器,其页面创建速度很慢。
You still have many of the typical problems with concurrency correctness however regarding shared data, but the deterministic task switching makes it easier to write safe code since you know exactly where control will be transferred and therefore know the exact points at which the shared state must be up to date.
然而,关于共享数据,您仍然存在许多并发正确性的典型问题,但确定性任务切换使编写安全代码变得更容易,因为您确切地知道控制将转移到哪里,因此知道共享状态必须在的确切点最新。
回答by zeroDivisible
Thirler already mentioned that stackless was used in Eve Online. Keep in mind, that:
Thirler 已经提到过在 Eve Online 中使用了 stackless。请记住:
(..) stackless adds a further twist to this by allowing tasks to be separated into smaller tasks, Tasklets, which can then be split off the main program to execute on their own. This can be used for fire-and-forget tasks, like sending off an email, or dispatching an event, or for IO operations, e.g. sending and receiving network packets. One tasklet waits for a packet from the network while others continue running the game loop.
It is in some ways like threads, but is non-preemptive and explicitly scheduled, so there are fewer issues with synchronization. Also, switching between tasklets is much faster than thread switching, and you can have a huge number of active tasklets whereas the number of threads is severely limited by the computer hardware.
(..) stackless 通过允许将任务分成更小的任务 Tasklets 进一步增加了这一点,Tasklets 然后可以从主程序中分离出来单独执行。这可用于即发即弃的任务,例如发送电子邮件或调度事件,或用于 IO 操作,例如发送和接收网络数据包。一个 tasklet 等待来自网络的数据包,而其他 tasklet 继续运行游戏循环。
它在某些方面类似于线程,但它是非抢占式和显式调度的,因此同步问题较少。此外,tasklet 之间的切换比线程切换要快得多,并且您可以拥有大量活动的 tasklet,而线程的数量受到计算机硬件的严重限制。
(got this citation from here)
(从这里得到这个引文)
At PyCon 2009 there was given a very interesting talk, describing why and how Stackless is used at CCP Games.
在 PyCon 2009 上,有一个非常有趣的演讲,描述了为什么以及如何在 CCP Games 中使用 Stackless。
Also, there is a very good introductory material, which describes why stackless is a good solution for Your applications. (it may be somewhat old, but I think that it is worth reading).
此外,还有一个非常好的介绍材料,它描述了为什么 stackless 是您的应用程序的一个很好的解决方案。(可能有些旧,但我认为值得一读)。
回答by Thirler
EVEOnline is largely programmed in Stackless Python. They have several dev blogs on the use of it. It seems it is very useful for high performance computing.
EVEOnline 主要是用 Stackless Python 编写的。他们有几个关于使用它的开发博客。它似乎对高性能计算非常有用。
回答by rdw
While I've not used Stackless itself, I have used Greenlet for implementing highly-concurrent network applications. Some of the use cases Linden Lab has put it towards are: high-performance smart proxies, a fast system for distributing commands over huge numbers of machines, and an application that does a ton of database writes and reads (at a ratio of about 1:2, which is very write-heavy, so it's spending most of its time waiting for the database to return), and a web-crawler-type-thing for internal web data. Basically any app that's expecting to have to do a lot of network I/O will benefit from being able to create a bajillion lightweight threads. 10,000 connected clients doesn't seem like a huge deal to me.
虽然我没有使用 Stackless 本身,但我已经使用 Greenlet 来实现高并发网络应用程序。Linden Lab 的一些用例包括:高性能智能代理、用于在大量机器上分发命令的快速系统,以及执行大量数据库写入和读取的应用程序(比率约为 1 :2,这是非常写重的,所以它大部分时间都在等待数据库返回),以及用于内部网络数据的网络爬虫类型的东西。基本上,任何需要进行大量网络 I/O 的应用程序都将从能够创建大量轻量级线程中受益。10,000 个连接的客户端对我来说似乎没什么大不了的。
Stackless or Greenlet aren't really a complete solution, though. They are very low-level and you're going to have to do a lot of monkeywork to build an application with them that uses them to their fullest. I know this because I maintain a library that provides a networking and scheduling layer on top of Greenlet, specifically because writing apps is so much easier with it. There are a bunch of these now; I maintain Eventlet, but also there is Concurrence, Chiral, and probably a few more that I don't know about.
但是,Stackless 或 Greenlet 并不是真正的完整解决方案。它们是非常低级的,您将不得不做大量的猴子工作来使用它们构建一个可以充分利用它们的应用程序。我知道这一点是因为我维护了一个在 Greenlet 之上提供网络和调度层的库,特别是因为使用它编写应用程序要容易得多。现在有很多这样的;我维护 Eventlet,但也有 Concurrence、Chiral,可能还有一些我不知道的。
If the sort of app you want to write sounds like what I wrote about, consider one of these libraries. The choice of Stackless vs Greenlet is somewhat less important than deciding what library best suits the needs of what you want to do.
如果您要编写的应用程序类型听起来像我写的那样,请考虑使用这些库之一。Stackless 与 Greenlet 的选择比决定哪个库最适合您想要做的事情更重要。
回答by porgarmingduod
The basic usefulness for green threads, the way I see it, is to implement a system in which you have a large amount of objects that do high latency operations. A concrete example would be communicating with other machines:
在我看来,绿色线程的基本用途是实现一个系统,在该系统中您拥有大量执行高延迟操作的对象。一个具体的例子是与其他机器通信:
def Run():
# Do stuff
request_information() # This call might block
# Proceed doing more stuff
Threads let you write the above code naturally, but if the number of objects is large enough, threads just cannot perform adequately. But you can use green threads even for in really large amounts. The request_information()
above could switch out to some scheduler where other work is waiting and return later. You get all the benefits of being able to call "blocking" functions as if they return immediatelywithout using threads.
线程可以让你自然地编写上面的代码,但是如果对象的数量足够大,线程就无法充分执行。但是,即使数量非常大,您也可以使用绿色线程。在request_information()
上面可以切换出一些调度,而其他的工作在等待,稍后再回来。您可以获得能够调用“阻塞”函数的所有好处,就好像它们在不使用线程的情况下立即返回一样。
This is obviously very useful for any kind of distributed computing if you want to write code in a straightforward way.
如果您想以直接的方式编写代码,这对于任何类型的分布式计算显然都非常有用。
It is also interesting for multiple cores to mitigate waiting for locks:
多核减少等待锁也很有趣:
def Run():
# Do some calculations
green_lock(the_foo)
# Do some more calculations
The green_lock
function would basically attempt to acquire the lock and just switch out to a main scheduler if it fails due to other cores using the object.
该green_lock
函数基本上会尝试获取锁,如果由于其他内核使用该对象而失败,则切换到主调度程序。
Again, green threads are being used to mitigate blocking, allowing code to be written naturally and still perform well.
同样,绿色线程被用于缓解阻塞,允许自然编写代码并仍然表现良好。