Interprocess communication with SPSC queue in python

天涯浪子 提交于 2020-01-06 05:22:09

问题


I have multiple write-heavy Python applications (producer1.py, producer2.py, ...) and I'd like to implement an asynchronous, non-blocking writer (consumer.py) as a separate process, so that the producers are not blocked by disk access or contention.

To make this more easily optimizable, assume I just need to expose a logging call that passes a fixed length string from a producer to the writer, and the written file does not need to be sorted by call time. And the target platform can be Linux-only. How should I implement this with minimal latency penalty on the calling thread?

This seems like an ideal setup for multiple lock-free SPSC queues but I couldn't find any Python implementations.


Edit 1

I could implement a circular buffer as a memory-mapped file on /dev/shm, but I'm not sure if I'll have atomic CAS in Python?


回答1:


The simplest way would be using an async TCP/Unix Socket server in consumer.py.
Using HTTP will be an overhead in this case.

A producer, TCP/Unix Socket client, will send data to consumer then consumer will respond right away before writing data in disk drive.

File IO in consumer are blocking but it will not block producers as stated above.



来源:https://stackoverflow.com/questions/47547731/interprocess-communication-with-spsc-queue-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!