The queue of concurrent programming threads, the thread pool; And the co-process (iv)

Source: Internet
Author: User

Thread:

  Queues: Queue
From queue import queue

Characteristics:
Advanced First Out

Self-locking, data security
Method:
Put () add
Get () gets

Put_nowait ()

Get_nowait ()

Full ()

Empty ()

Qsize ()

As with normal queue usage, see the process queue for details


  Stack : lifoqueue
From queue import Lifoqueue


Last in, first out

Self-locking, data security

  Priority Queue :P Riorityqueue
From queue import Priorityqueue

The Put ((TULP)) parameter accepts a tuple, which is two elements, the first an int that represents a priority, and the smaller the int, the greater the precedence

  thread pool : from concurrent.futures import threadpoolexecutor

1, threading no thread pool related content, at the back of the time, someone wrote a concurrent.futures to help us manage thread/process pool

Process pools and thread pools in the Concurrent.futures module
Highly encapsulated
How the process/thread pool is used uniformly:

co-process :

  Summary : On a thread basis, switch between multiple tasks
Saves thread-on-time consumption, and the co-process is dispatched from the Python code level.

  yield:
1, the match between yield and next () is one of the most basic co-processes.
2, only switch between programs, no time to re-use any IO operation

  Third-party module: Greenlen
From Greenlet import Greenlet

This module is used to help us to perform transitions between the threads.
Method:
Switch () toggle

co-process content

  Third-party module: gevent
Import Gevent

This module is based on the Greenlen module developed by an auto-encounter block is switched module (this is the artifact)
At the same time, this mechanism takes full advantage of the idle time generated by IO operations. Make the program faster
Spawn (FU) set the FU function to co-process
Sleep () clogging
Join () Jam
Joinall ([list]) waits for all the threads in the list to complete and unblock.

  Note : Gevent only know their own blockage status, if you want to let them know other needs with the help of monkey

  Help Gevent recognize other blockages: Monkey.patch_all ()
From gevent import Monkey;monkey.patch_all () write before other modules are imported

Monkey.patch_all () is to record the blockage status of all the modules below

1 #use the co-process to reduce the time consumed by IO operations2  fromGeventImportMonkey;monkey.patch_all ()3 Importgevent4 Import Time5 6 defeat ():7     Print('Eat')8Time.sleep (2)9     Print('Finished eating')Ten  One defPlay (): A     Print('Play') -Time.sleep (1) -     Print('It's a beautiful play.') the  -G1 =gevent.spawn (Eat) -G2 =Gevent.spawn (play) - Gevent.joinall ([g1,g2]) + #G1.join () - #G2.join () + #not executed A #Why didn't you do??? Do you need to open it? at #It didn't turn on, but it switched. -     #gevent help you to do the switch, do the switch is conditional, encountered IO to switch -     #gevent does not recognize IO operations other than gevent in this module -     #use join to block until the process task finishes - #help Gevent to understand blocking in other modules -     #from gevent import Monkey;monkey.patch_all () write before other modules are imported
gevent

The queue of concurrent programming threads, the thread pool; And the co-process (iv)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.