Threading Introduction:
If you want your application to make better use of the computational resources of multi-core machines, you is advised to Use multiprocessing. However, threading is still a appropriate model if you want to run multiple i/o-bound tasks simultaneously
Multithreading with IO-intensive operations on a single-core CPU can improve performance, multi-core CPU multithreading is meaningless, and performance gains are not achieved. Because the GIL (the Python global Interpreter Lock)causes a maximum of one thread per python process to run at the same time, Python multithreaded programs do not improve program performance and do not have the advantage of multicore systems.
Collections.deque is a two-way list for efficient insert and delete operations, suitable for queues and stacks.
Although Python cannot implement multi-core tasks with multithreading, multi-core tasks can be achieved through multiple processes. Multiple Python processes have separate Gil locks that do not affect each other.
Multiprocessing. The queue is a high-performance stack introduced by Python 2.6 to implement multiple processes.
The queue is a typical producer-consumer model.
Producers produce goods and then put the goods into a data structure such as a queue, the time required to produce the goods is uncertain, and the time that consumers consume the goods produced by the producers is not certain.
Queues are particularly useful when threads are securely exchanging information between multithreaded
The queue module can be used to communicate between threads, allowing data to be shared between threads .
The following explains the differences between stacks and queues:
1. Stack (stacks) is a linear data structure that can only be accessed by accessing the first end of the database, and has the characteristics of the LIFO (last Out,lifo)
2. A queue is a linear data structure with FIFO features, where the addition of elements can only be done at one end, and the deletion of elements can only take place at the other end. The one end of the queue that can increment the element is called the tail of the queue, and the one end of the element is called the team head.
Here are the official examples:
Using Lists as Stacks
The list methods make it very-a list as a stack, where the last element added is the first element retrieved ( "Last-in, first-out"). To add a item to the top of the stack, use append (). To retrieve a item from the top of the stack, use pop () without an explicit index. For example:
>>> stack = [3, 4, 5]
>>> Stack.append (6)
>>> Stack.append (7)
>>> Stack
[3, 4, 5, 6, 7]
>>> Stack.pop ()
7
>>> Stack
[3, 4, 5, 6]
>>> Stack.pop ()
6
>>> Stack.pop ()
5
>>> Stack
[3, 4]
Using Lists as Queues
It is also possible to use a list as a queue, where the first element added is the first element retrieved ("First-in, fir St-out "); However, lists is not the efficient for this purpose. While appends and POPs from the end of the list was fast, doing inserts or POPs from the beginning of a list is slow (because All of the other elements has to be shifted by one).
To implement a queue, use Collections.deque which is designed to has fast appends and POPs from both ends. For example:
>>> from collections Import deque
>>> queue = Deque (["Eric", "John", "Michael"])
>>> queue.append ("Terry") # Terry arrives
>>> queue.append ("Graham") # Graham arrives
>>> Queue.popleft () # The first to arrive now leaves
' Eric '
>>> Queue.popleft () # The second to arrive now leaves
' John '
>>> Queue # Remaining queue in order of arrival
Deque ([' Michael ', ' Terry ', ' Graham '])
Python multi-threaded, multi-process, queue notes (i)