Recently, I use Python to develop web programs, and I have always used fastcgi mode. Then multiple threads are started in each process to process requests. One problem here is that it needs to ensure that the response time of each request is very short, otherwise it will only take multiple requests. A few slowdowns will cause the server to refuse service because no thread can respond to the request. We usually conduct performance tests when our services come online, so there is not much of a problem under normal circumstances. But it is impossible to test all scenarios. Once it occurs, It will make the user wait for a long time without responding. Some parts are unavailable, which leads to all unavailability. Later, I switched to coroutine and greenlet under python. So I have a simple understanding of its implementation mechanism.
Each greenlet is just a python in the heap object (PyGreenlet). So it is no problem for you to create millions or even tens of millions of greenlets for a process.
typedef struct _greenlet { PyObject_HEAD char* stack_start; char* stack_stop; char* stack_copy; intptr_t stack_saved; struct _greenlet* stack_prev; struct _greenlet* parent; PyObject* run_info; struct _frame* top_frame; int recursion_depth; PyObject* weakreflist; PyObject* exc_type; PyObject* exc_value; PyObject* exc_traceback; PyObject* dict; } PyGreenlet;
Each greenlet is actually a function, and saves the context when this function is executed. For a function, the context is its stack.. All greenlets in the same process share a common user stack allocated by the operating system. Therefore, only greenlets with non-conflicting stack data can use this global stack at the same time. Greenlets save their stacks through stack_stop and stack_start. At the bottom and top of the stack, if the stack_stop of the greenlet to be executed overlaps with the greenlet currently in the stack, the stack data of these overlapping greenlets must be temporarily saved to the heap. The saved location is determined by stack_copy and stack_saved. Record so that the positions of stack_stop and stack_start in the stack can be copied from the heap back to the stack during recovery. Otherwise, the stack data will be destroyed. Therefore, these greenlets created by the application are constantly copying data to the heap or from The heap is copied to the stack to achieve concurrency. It is really comfortable to use coroutine for IO-type applications.
The following is a simple stack space model of greenlet (from greenlet.c)
A PyGreenlet is a range of C stack addresses that must be saved and restored in such a way that the full range of the stack contains valid data when we switch to it. Stack layout for a greenlet: | ^^^ | | older data | | | stack_stop . |_______________| . | | . | greenlet data | . | in stack | . * |_______________| . . _____________ stack_copy + stack_saved . | | | | . | data | |greenlet data| . | unrelated | | saved | . | to | | in heap | stack_start . | this | . . |_____________| stack_copy | greenlet | | | | newer data | | vvv |
The following is a simple paragraph greenlet code.
from greenlet import greenlet def test1(): print 12 gr2.switch() print 34 def test2(): print 56 gr1.switch() print 78 gr1 = greenlet(test1) gr2 = greenlet(test2) gr1.switch()

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools