1. The meaning and realization of the association Process
The co-process is a scheduling mechanism of the transcendental function of single-session single-thread, which is dispatched by certain dispatching means.
(Python uses the generator mechanism, Greenlet uses assembly control to point to the program).
2. What is the role of the co-process
The computer is divided into IO bound and CPU bound two types of task. In both cases, the co-process has little effect.
Why?
In the CPU bound task, the CPU is used to perform the task. This kind of task, even if a method of execution, and the efficiency of the process is a little higher, the use of the process is meaningless. IO bound task, the CPU has been caught in the system call, user space scheduling is no CPU, in this case, the association can only be dead. In such cases, pray for high efficiency, how possible.
Only in the case of a very limited system, there are some uses, in the single-process one-way task of the interactive Green Xia, only its use.
3. The process is not the future (rebuttal Rai Yonghao). The co-process was a long time ago. Early on, Windows had a fiber concept, and Linux was not sure. But it always exists as a niche API.
4. Two reviews of the Association process:
ImportgeventImportRandomImport Timedeftask (PID):"""Some non-deterministic Task""" forIinchRange (1000): Random.randint (0,20)defsynchronous (): forIinchRange (1,10000): Task (i)defasynchronous (): Threads= [Gevent.spawn (Task, I) forIinchXrange (10000)] Gevent.joinall (threads)Print('Synchronous:') T=Time.time () Synchronous () diff= Time.time ()-TPrint "diff is%f"%diffPrint('Asynchronous:') T=Time.time () Asynchronous () diff= Time.time ()-TPrint "diff is%f"%diff
Results:
is 22.333482 are 22.422071
2.IO bound
ImportGevent.monkeygevent.monkey.patch_socket ()ImportgeventImportUrllib2Import Timedeffetch (PID): Response= Urllib2.urlopen ('http://127.0.0.1:8080/') Result=Response.read ()returnresultdefsynchronous (): forIinchRange (1,100): Fetch (i)defasynchronous (): Threads= [] forIinchRange (1,100): Threads.append (Gevent.spawn (Fetch, i)) Gevent.joinall (threads)Print('Synchronous:') T=Time.time () Synchronous () diff= Time.time ()-TPrint "diff is%f"%diffPrint('Asynchronous:') T=Time.time () Asynchronous () diff= Time.time ()-TPrint "diff is%f"%diff
The result is:
is 0.791572 are 0.997519
The result of the above example is only a single run result, which is very relevant to the machine used.
5.gevnet+flask is a very popular usage, but gevent really useful is Libev, which is using Epoll (Linux), Kqueue (BSD), IOCP (Windows) to implement network IO, and in polling processing signal , Signal,callback Lib.
End ... Opinion, welcome to shoot bricks.
Role of the Association Python