Network programming of concurrent programming - queue
Queue Introduction
Isolated from each other processes, to achieve inter-process communication (IPC), multiprocessing module supports two forms: the queue and the pipeline, these two methods are the use of messaging.
Create a class queue (and the pipe bottom is in locking manner to):
Queue([maxsize]):创建共享的进程队列,Queue是多进程安全的队列,可以使用Queue实现多进程之间的数据传递。
Parameter Description:
maxsize是队列中允许最大项数,省略则无大小限制。
但需要明确:
1、队列内存放的是消息而非大数据。
2、队列占用的是内存空间,因而maxsize即便是无大小限制也受限于内存大小。
The main method of introduction:
q.put方法用以插入数据到队列中。
q.get方法可以从队列读取并且删除一个元素。
Use the queue:
from multiprocessing import Process,Queue
q=Queue(3)
#put ,get ,put_nowait,get_nowait,full,empty
q.put(1)
q.put(2)
q.put(3)
print(q.full()) #满了
# q.put(4) #再放就阻塞住了
print(q.get())
print(q.get())
print(q.get())
print(q.empty()) #空了
# print(q.get()) #再取就阻塞住了