简体   繁体   English

同时使用读取和写入文件运行Python和C ++

[英]Simultaneous Python and C++ run with read and write files

So this one is a doozie, and a little too specific to find an answer online. 因此,这是个笨蛋,有点太具体了,无法在线找到答案。

I am writing to a file in C++ and reading that file in Python at the same time to move a robot. 我正在用C ++写入文件,并同时在Python中读取该文件以移动机器人。 Or trying to. 或尝试。

When I try running both programs at the same time, the C++ one runs first and then the Python one runs. 当我尝试同时运行两个程序时,首先运行C ++,然后运行Python。

Here's the command I use: 这是我使用的命令:

./ColorFollow & python fileToHex.py

This happens even if I switch the order of commands. 即使我切换命令顺序也会发生这种情况。 Even if I run them in different terminals (which is the same thing, just covering all bases). 即使我在不​​同的终端上运行它们(这是同一件事,只是涵盖了所有基础)。

Both the Python and C++ code read / write in 'infinite' loops, so these two should run until I say stop. Python和C ++代码都在“无限”循环中读/写,因此这两个都应该运行直到我说停止为止。

The code works fine; 代码工作正常; when the Python script finally runs the robot moves as intended. 当Python脚本最终运行时,机器人会按预期移动。 It's just that the code doesn't run at the same time. 只是代码不能同时运行。

Is there a way to make this happen, or is this impossible? 有没有办法做到这一点,或者这是不可能的?

If you need more information, lemme know, but the code is pretty much what you'd expect it to be. lemme知道,如果您需要更多信息,但是代码几乎与您期望的一样。

If you are using Linux, & will release bash session and in this case, CollorFlow and fileToXex.py will run in different bash sessions. 如果使用Linux,则&将释放bash会话,在这种情况下,CollorFlow和fileToXex.py将在不同的bash会话中运行。
At the same time, composition ./ColorFollow | python fileToHex.py 同时,组成./ColorFollow | python fileToHex.py ./ColorFollow | python fileToHex.py looks interesting, cause you redirect stdout of ColorFollow to fileToHex.py stdin - it can syncronize scripts by printing some code string upon exit, then reading it by fileToHex.py and exit as well. ./ColorFollow | python fileToHex.py看起来很有趣,因为您将ColorFollow的stdout重定向到fileToHex.py stdin-它可以通过在退出时打印一些代码字符串来同步脚本,然后通过fileToHex.py读取并退出。
I would create some empty file like /var/run/ColorFollow.flag and write there 1 when one of processes exit. 我会创建一些空文件,例如/var/run/ColorFollow.flag并在其中一个进程退出时在其中写入1。 Not a pipe - cause we do not care which process will start first. 不是管道-因为我们不在乎哪个过程将首先开始。 So, if next loop step of ColorFollow sees 1 in the file, it deletes it and exits (means that fileToHex already exited). 因此,如果ColorFollow的下一个循环步骤在文件中看到1,它将删除该文件并退出(意味着fileToHex已经退出)。 The same - for fileToHex - check flag file each loop step and exit if it exists, after deleting flag file. 相同-对于fileToHex-在删除标记文件后,在每个循环步骤中检查标记文件并退出(如果存在)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM