[英]Wait for signal to start generating data from another process in python
I have two independent processes in python: producer and consumer . 我在python中有两个独立的进程: 生产者和消费者 。 Producer generates files and consumer does some processing on files.
生产者生成文件,而使用者对文件进行一些处理。
To test both applications, I find myself constantly starting two programs, and producer has a delay function, etc. which is a pain. 为了测试这两个应用程序,我发现自己不断地启动两个程序,并且生产者具有延迟功能等,这很痛苦。
What is the quickest way to implement some kind of signaling machinery in python so that consumer says "GO!" 在python中实现某种信令机制以使消费者说“ GO!”的最快方法是什么? and producer starts doing things it does.
生产者开始做它做的事情。
This way I can have producer running all the time. 这样,我可以让生产者一直运行。
One simple way to do this (you didn't mention what platform(s) you care about, so I'll assume you want something cross-platform) is a UDP socket with a known port. 一种简单的方法(您没有提到您关心的平台,所以我假设您需要跨平台的东西)是带有已知端口的UDP套接字。 If the consumer just listens on localhost port 12345, it can block on a
sock.recvfrom
call every time it needs to wait, and the producer can do a sock.sendto
to notify it. 如果使用者仅在本地主机端口12345上侦听,则它可以在每次需要等待时阻塞
sock.recvfrom
调用,而生产者可以执行sock.sendto
来通知它。
For example, here's a consumer: 例如,这是一个消费者:
#!/usr/bin/env python
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('127.0.0.1', 12345))
while True:
dummy, addr = sock.recvfrom(1024)
# work on files until done
And a producer: 和制作人:
#!/usr/bin/env python
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
for thing in things:
# produce files for consumer
sock.sendto(b'X', ('127.0.0.1', 12345))
Other things to consider: 要考虑的其他事项:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.