python 使用 linux 系统调用实现进程间通信

前言

python multiprocessing中的queue实现进程间通信也是可以,但是在使用过程中遇到了一些问题. 处于性能考虑,思考使用Linux环境下的系统调用来进行进程间通信.本想自己实现一个Python的第三方拓展. 仔细考察一番后,发现已经有一个第三方库实现, posix-ipcsysv-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 系统调用实现进程间通信

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注

  1. jordan 4 说道:

    I definitely wanted to type a quick comment so as to appreciate you for all the amazing guidelines you are giving on this site. My rather long internet investigation has now been recognized with professional strategies to write about with my family members. I 'd declare that most of us website visitors actually are truly lucky to exist in a superb community with many outstanding professionals with valuable advice. I feel somewhat fortunate to have used your weblog and look forward to plenty of more cool moments reading here. Thanks once more for a lot of things.

  2. supreme new york 说道:

    I and also my buddies ended up examining the nice tips and tricks from the website and then before long came up with a terrible feeling I never expressed respect to you for those strategies. These young men had been absolutely stimulated to see all of them and now have unquestionably been having fun with them. Thank you for being indeed considerate and also for considering such exceptional guides most people are really desperate to learn about. My honest regret for not expressing appreciation to you earlier.

  3. yeezy shoes 说道:

    I precisely needed to thank you very much again. I am not sure what I could possibly have worked on without these solutions provided by you about my question. This was a real scary issue in my circumstances, but considering the very professional avenue you handled that took me to cry over joy. I'm happy for your work and thus wish you find out what a powerful job your are accomplishing teaching the mediocre ones via your webblog. Probably you have never got to know all of us.

  4. surpreme 说道:

    I would like to express my appreciation for your kind-heartedness in support of men who must have guidance on your content. Your special dedication to passing the message up and down turned out to be incredibly useful and has constantly enabled some individuals just like me to reach their aims. Your personal interesting help can mean a whole lot a person like me and additionally to my fellow workers. Thanks a ton; from each one of us.

  5. jordan shoes 说道:

    I'm commenting to make you understand of the nice discovery our princess encountered checking the blog. She noticed several things, most notably how it is like to have an awesome giving heart to make many people quite simply thoroughly grasp chosen hard to do subject matter. You truly exceeded her expectations. Thanks for churning out these important, trusted, explanatory and also easy guidance on your topic to Sandra.

  6. supreme clothing 说道:

    Needed to draft you that bit of note just to say thank you the moment again regarding the nice solutions you've documented at this time. It has been simply unbelievably open-handed of you to grant freely precisely what a lot of folks might have offered as an e-book to help make some profit for themselves, mostly since you might have done it in the event you desired. These good tips likewise served to be the great way to be aware that some people have similar fervor just like my personal own to learn much more with regards to this problem. I believe there are some more pleasurable occasions ahead for folks who scan your blog post.

  7. hdfqfzfti 说道:

    出发地 hdfqfzfti http://www.ga2fna7df09s811p9v1s023w1km266nds.org/ [url=http://www.ga2fna7df09s811p9v1s023w1km266nds.org/]uhdfqfzfti[/url] ahdfqfzfti

  8. kd 12 说道:

    I precisely needed to appreciate you again. I do not know the things that I might have made to happen in the absence of those solutions provided by you regarding that industry. It actually was a challenging setting in my position, nevertheless being able to view a new professional form you handled it forced me to jump with contentment. I'm just thankful for the assistance and in addition hope that you are aware of an amazing job you're getting into teaching people with the aid of a web site. Most probably you have never got to know any of us.