简体   繁体   English

实时将变量从python传递到C的最佳方法

[英]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: 您可以通过多种方式执行此操作:

  1. Signals - send SIGUSR1 signal to player from Python. 信号-从Python向播放器发送SIGUSR1信号。
  2. Sockets - player listens on some port and Python sends some data on that port (UDP will be easiest in that case). 套接字-播放器在某个端口上侦听,而Python在该端口上发送一些数据(在这种情况下,UDP最简单)。
  3. Fifo - there is mkfifo on Linux machines. Fifo-Linux机器上有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.

相关问题 使用CFFI从Python传递指向C函数的指针的最佳方法 - Best way to pass pointer to C function from Python using CFFI (Python) 如何在“实时”中将变量从 file1.py 传递到 file2.py? - (Python) How to pass a variable from file1.py to file2.py in "REAL TIME"? Python-将变量参数传递给subprocess.call的最佳方法 - Python- best way to pass variable arguments to subprocess.call 实时更改 tkinter 小部件的最佳方法 - Best way to change tkinter widgets real time 如何将实时处理的数据从C#共享到python - How to share real time processed data from C# to python 从C ++ exe获取输出并在Python中实时处理它 - Take output from C++ exe and work on it in Python in real time 将数据从Python传递到PHP的最佳方法是什么 - What is the best way to pass data from Python to PHP GAE / Python-将请求从控制器传递到模型的最佳方法是什么? - GAE/Python - What is the best way to pass the request from the controller to the model? 有没有办法将变量传递给 class 并在 Python 中从中创建一个 class 变量? - Is there a way to pass a variable to a class and create a class variable from it in Python? 从网页上的SSH可访问文件中显示实时数据的最佳方法 - Best way to display real-time data from an SSH accesible file on webpage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM