前言
python multiprocessing中的queue实现进程间通信也是可以,但是在使用过程中遇到了一些问题.
处于性能考虑,思考使用Linux环境下的系统调用来进行进程间通信.本想自己实现一个Python的第三方拓展.
仔细考察一番后,发现已经有一个第三方库实现, posix-ipc
和 sysv-ipc
的实现.
由于实际的工作环境是Linux,且考虑到易用性,最终选择了 posix-ipc
来解决问题.
安装 posix-ipc
posix-ipc提供了共享内存、消息队列、信号量相关。
pip install posix-ipc
使用
该第三方库,源码中提供了比较完善的demo源码.此处仅仅举例使用消息队列.
消息队列消费实现
import posix_ipc
import selectors
# This program uses `posix_ipc` together with the `selectors`library from the
# Python standard library. `selectors` provides "high-level I/O multiplexing" akin to having an event library.
# The message queue is created as usual
mq = posix_ipc.MessageQueue("/python_ipc_test", flags=posix_ipc.O_CREAT)
mq.block = False
# Function is defined to handle events on the queue
def accept(message_queue, mask):
(msg, prio) = message_queue.receive()
print("Message: ", msg)
print("Priority: ", prio)
# The selector can now be created...
sel = selectors.DefaultSelector()
# ... and the message queue is registered. Other event sources could also be
# registered simultaneously, but for now we stick to the queue
sel.register(mq, selectors.EVENT_READ, accept)
# `.select()` will block until an event is triggered
# for i in range(10000*3+1):
events = sel.select()
for key, mask in events:
# `.data` contains the third argument from `.register` above -- we use it for the callback.
callback = key.data
callback(key.fileobj, mask)
# With the message successfully received, we can unlink and close.
mq.unlink()
mq.close()
队列生产实现
import posix_ipc
# This program opens the message queue and sends a message
mq = posix_ipc.MessageQueue("/python_ipc_test")
mq.block = True
import time
st = time.time()
print(f"start time {st}")
# for i in range(270000):
mq.send("From transmitter")
ed = time.time()
print(f"end time {ed}, use {ed - st} .")
版权声明:除特别注明外,本站所有文章均为王晨曦个人站点原创
转载请注明:出处来自王晨曦个人站点 » python 使用 linux 系统调用实现进程间通信
出发地 akdlfdxmik [url=http://www.g89o5w65y7mvjil745xwuen0133184gps.org/]ukdlfdxmik[/url] kdlfdxmik http://www.g89o5w65y7mvjil745xwuen0133184gps.org/