Gevent可以与Python中的真实线程结合使用吗?

时间:2013-05-29 10:54:05

标签: python multithreading gevent coroutine

只是想知道gevent coro-thread是否可以使用真正的线程?也许我可以编写一个包含多个线程的程序,每个线程中都有一些协同程序?

编辑

gevent.Threading和CPython线程都有其不足之处。协程不能使用多个CPU,而当线程数量很大时,CPython线程性能受到GIL的限制。 我也在开发类似于bluelet的纯Python协程实现(尽管开销可能比gevent大),以增加同时使用coro和线程的能力。

我想知道gevent或greenlet(https://pypi.python.org/pypi/greenlet)是否可以通过CPython线程实现合作能力。

1 个答案:

答案 0 :(得分:3)

Gevent 1.0应该支持每个线程都有一个Gevent主循环,如果这就是你的意思。

此外,gevent.threadpool允许以与gevent兼容的方式在实际线程中运行任务:

threadpool = gevent.threadpool.ThreadPool()
result = threadpool.spawn(some_non_gevent_friendly_thing_such_as_file_io)

result.get()  # or use ThreadPool.apply/apply_e to get the result value immediately

您可以通过查看https://github.com/gevent/gevent/blob/master/gevent/threadpool.py(或者一旦1.0的文档以已发布/ HTML格式提供)获得有关gevent.threadpool的更多信息。