简体   繁体   English

从另一个 Python 文件启动 Python 文件然后在它们同时运行时通过它们发送数据的最简单方法是什么?

[英]What is the easiest way to launch a Python file from another Python file then send data across them while they run simultaneously?

I want to first run a python file(parent) which automatically executes another python file(child) and send data(specifically a list) from the parent file to the child file.我想先运行一个 python 文件(父),它会自动执行另一个 python 文件(子)并将数据(特别是列表)从父文件发送到子文件。

I cannot import one file into another since the parent file has to execute a lot of blocking commands and I need the child file to publish continuously.我无法将一个文件导入另一个文件,因为父文件必须执行大量阻塞命令并且我需要子文件连续发布。

I initially tried Popen subprocess with which I was able to run both at the same time but I wasn't able to catch any output from the parent in the child class. Simply writing data to a file then reading it from the child class could work but I wasn't able to get it working reliably since it only read once;我最初尝试了 Popen 子进程,我可以使用它同时运行这两个进程,但是我无法从子进程 class 中的父进程中捕获任何 output。只需将数据写入文件然后从子进程 class 读取它就可以了但我无法让它可靠地工作,因为它只读了一次; and closing the file to open it again to read seemed way too hacky and slow.并关闭文件以再次打开它以阅读似乎太老套和缓慢了。

So how is this normally done?那么这通常是如何完成的呢? Here's a super-simplified version of the code:-这是代码的超级简化版本:-

File 1(Parent file):-文件 1(父文件):-

list = [0,0,0]
class CmdDecide:
    def __init__(self, param1=50):
        //Some code
        //I tried subprocess.run(['python3',file2.py]) to run the 2 files simultaneously but I need to send the list variable to it
    
    def func_A(self, var_from_someplace_else):
        global list
        list = var_from_someplace_else
        sendList(list)
    
    def sendList(self):
        //Code to send this list to file 2 somehow
    
    //There are a lot more functions in this file

File 2(Child file):-文件 2(子文件):-

def main():
    //Send continuous output [0,0,0] till File 1 changes the array
    //It will continuously output the new array and continue to do so till the program is terminated

Note: Ik this exists Run one Python code in the background from another Python code but the pipe has not been implemented and I'm not sure if it can even handle lists.注意:Ik this exists 在后台从另一个 Python 代码运行一个 Python 代码,但 pipe 尚未实现,我不确定它是否可以处理列表。

If the main concern of merging the two files into one is blocking processes then run the parent file methods as async ( https://docs.python.org/3/library/asyncio.html ).如果将两个文件合并为一个文件的主要问题是阻塞进程,那么将父文件方法作为异步运行 ( https://docs.python.org/3/library/asyncio.html )。 Invoking one file as a sub-process is not in my opinion the best way to go forwards as it decouples the logic but maintains the dependency.在我看来,将一个文件作为子流程调用并不是 go 转发的最佳方式,因为它解耦了逻辑但保持了依赖性。 If you need to invoke the file as a sub-process then look into argpase and pass the list as a argument to如果您需要将文件作为子进程调用,则查看argpase并将列表作为参数传递给

subprocess.run(['python3',file2.py])

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

相关问题 在Python中从文件加载数组数据的最简单方法是什么? - what's the easiest way to load array data from a file in Python? 从Python读取FoxPro DBF文件最简单的方法是什么? - What's the easiest way to read a FoxPro DBF file from Python? 在python中将数据结构持久化到文件的最简单方法? - Easiest way to persist a data structure to a file in python? 将数据从一个 python 文件发送到另一个文件 - Send data from one python file to another python 中修改 linux 配置文件的最简单方法是什么? - What is the easiest way in python to modify linux config file? 读取由python中的tab分隔的文本文件的最简单方法是什么? - What is the easiest way to read the text file delimited by tab in python? 从Apache运行python脚本的最简单方法 - Easiest way to run python script from Apache 在Linux中从python显示pdf,ps或dvi文件的最简单方法 - Easiest way to display a pdf, ps or dvi file from python in linux 从共享全局变量的另一个python文件运行推荐序列的最佳方法是什么? - What's the best way to run a sequence of commend from another python file with global variables shared? Tensorflow Python-从纯数据(txt文件)创建NN的最简单方法 - Tensorflow Python - easiest way to create a NN from plain data (txt file)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM