[英]Best way to pass variable from python to C in real time
I have a media player I've created in C, and its running along side a server I have in Python. 我有一个用C创建的媒体播放器,它与我在Python中拥有的服务器一起运行。 The server accepts commands to modify a play list that resides in an SQLite database.
服务器接受命令来修改驻留在SQLite数据库中的播放列表。
Once the python code updates the DB (This happens in the background while the media player is playing) it needs to let the player know a new playlist is in the DB so it can start playing the new files. 一旦python代码更新了数据库(在播放媒体播放器时在后台发生),它需要让播放器知道数据库中有新的播放列表,以便它可以开始播放新文件。
I was going to set a flag in the DB, and the player can constantly poll this flag, but I don't think this is the best way. 我本打算在数据库中设置一个标志,并且播放器可以不断轮询该标志,但是我认为这不是最好的方法。 All the options I can think of involve the media player constantly checking for something.
我能想到的所有选项都涉及媒体播放器不断检查某些内容。
What would be the best way of going about this? 最好的方法是什么?
There are many ways you can do that: 您可以通过多种方式执行此操作:
mkfifo
on Linux machines. mkfifo
。 Files created that way works similar to sockets. I would recommend UDP sockets. 我会推荐UDP套接字。 It's easy, fast and more elegant than signals.
它比信号更容易,快速且更优雅。 Python code would be something about 3 lines of code.
Python代码大约是3行代码。
Example Python code: 示例Python代码:
sock = socket.socket()
sock.connect(('localhost', 9999))
sock.sendall('update')
sock.close()
Example C UDP server: http://www.cs.ucsb.edu/~almeroth/classes/W01.176B/hw2/examples/udp-server.c 示例C UDP服务器: http : //www.cs.ucsb.edu/~almeroth/classes/W01.176B/hw2/examples/udp-server.c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.