Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 什么是绿色?_Python_Gevent - Fatal编程技术网

Python 什么是绿色?

Python 什么是绿色?,python,gevent,Python,Gevent,我是gevent的新手。我读过gevent的简介 他们提供了简单的例子,但我很难理解什么是greenlet。通过学习并发性 Greenlets are a very lightweight coroutine written in C that are cooperatively scheduled. They provide us with a very lightweight thread- like object that allows us to achieve concurrent e

我是gevent的新手。我读过gevent的简介

他们提供了简单的例子,但我很难理解什么是greenlet。通过学习并发性

Greenlets are a very lightweight coroutine written in C that
are cooperatively scheduled. They provide us with a very lightweight thread-
like object that allows us to achieve concurrent execution within our Python
programs without incurring the cost of spinning up multiple threads.
绿叶不是线?
同步点是如何定义的?有人能举例说明吗?

同步编程一次只能做一件事。因此,当数据库查询运行时,其他所有人(比如说通过web框架打开网页)都必须等待查询完成

Gevent通过使用上下文切换和事件实现异步。这是什么意思?这样想吧。你有一个等待事情发生的队列,同时gevent说,好的,你可以等待,我将进入下一个任务,开始做一些事情,同时我正在等待你完成(如数据库读取,或等待用户输入),当你完成时,当我返回队列,你说你准备好进行下一步,我为你集中精力

通过这种方式,尽管仍然是单线程的,但应用程序可以在作业之间超快速切换,不断检查状态以确定它是否值得关注,同时,其他事情可以在它等待您时完成

与由操作系统和heavy处理的多线程不同,多线程需要自己的资源,并且在多线程之间切换成本很高


Gevent使得将通常使用线程的东西转换成greenlets变得容易

asyncio和gevent之间的区别是什么?asyncio适用于Python3,它是执行异步线程的另一种方式,非常类似于Javascript处理异步线程的方式。您有异步调用,等待回调并返回。我发现GEVENT的性能更高,编写代码也更容易10亿倍,因为基本Python代码基本保持不变。Gevent monkey修补了所有IO库,只是使它们异步。其余部分与任何正常的线程操作一样。我个人更喜欢gevent,但这只是一个偏好,而不是一个技术建议。两者都只是工具。