将值从一个Python程序传递到另一个Python程序

将值从一个Python程序传递到另一个Python程序,python,ipc,Python,Ipc,除了使用像.txt/dummy文件这样的文件之外,是否可能将值从一个程序传递到另一个程序 我有一个程序,它使用.txt文件将起始值传递给另一个程序。每次运行程序(十次,基本上是同时运行)时,我都会在启动程序之间更新文件中的值。这样做很好,但我想让“孩子”程序在完成后向“母亲”程序报告,并报告找到的下载文件 是否可以在不使用11个文件的情况下执行此操作(即“child”到“mother”报告的每个实例一个文件,而“mother”到“child”报告的每个实例一个文件)?我说的是完全独立的程序,不是

除了使用像.txt/dummy文件这样的文件之外,是否可能将值从一个程序传递到另一个程序

我有一个程序,它使用.txt文件将起始值传递给另一个程序。每次运行程序(十次,基本上是同时运行)时,我都会在启动程序之间更新文件中的值。这样做很好,但我想让“孩子”程序在完成后向“母亲”程序报告,并报告找到的下载文件

是否可以在不使用11个文件的情况下执行此操作(即“child”到“mother”报告的每个实例一个文件,而“mother”到“child”报告的每个实例一个文件)?我说的是完全独立的程序,不是类或函数之类的

为了高效地运行,而不是等待几个小时来完成所有的事情,我需要“child”程序运行十次,并更快地完成任务。因此,我运行子程序十次,并给每个程序一个单独的范围进行检查

这两个程序都运行得很好,但我想让它们彼此运行/报告,希望不要使用文件“传输”来完成任务,特别是在数据传输的子-母端

“母亲”计划…目前

import os
import sys
import subprocess
import time

os.chdir ('/media/')

#find highest download video
Hival = open("Highest.txt", "r") 
Histr = Hival.read()
Hival.close()
HiNext = str(int(Histr)+1)

#setup download #1
NextVal = open("NextVal.txt","w")
NextVal.write(HiNext)
NextVal.close()

#call download #1
procs=[]
proc=subprocess.Popen(['python','test.py'])
procs.append(proc)
time.sleep(2)

#setup download #2-11
Histr2 = int(Histr)/10000
Histr2 = Histr2 + 1

for i in range(10):
    Hiint = str(Histr2)+"0000"
    NextVal = open("NextVal.txt","w")
    NextVal.write(Hiint)
    NextVal.close()

    proc=subprocess.Popen(['python','test.py'])
    procs.append(proc)
    time.sleep(2)
    Histr2 = Histr2 + 1

for proc in procs:
    proc.wait()
“儿童”计划

import urllib
import os
from Tkinter import *
import time

root = Tk()
root.title("Audiodownloader")
root.geometry("200x200")

app = Frame(root)
app.grid()

os.chdir('/media/')
Fileval = open('NextVal.txt','r')
Fileupdate = Fileval.read()
Fileval.close()
Fileupdate = int(Fileupdate)
Filect = Fileupdate/10000
Filect2 = str(Filect)+"0009"
Filecount = int(Filect2)
while Fileupdate <= Filecount:
    root.title(Fileupdate)
    url = 'http://www.yourfavoritewebsite.com/audio/encoded/'+str(Fileupdate)+'.mp3'
    urllib.urlretrieve(url,str(Fileupdate)+'.mp3')
    statinfo = os.stat(str(Fileupdate)+'.mp3')
    if statinfo.st_size<10000L: 
        os.remove(str(Fileupdate)+'.mp3')

    time.sleep(.01)
    Fileupdate = Fileupdate+1
    root.update_idletasks()
导入urllib
导入操作系统
从Tkinter进口*
导入时间
root=Tk()
root.title(“音频下载器”)
根几何(“200x200”)
app=帧(根)
app.grid()
os.chdir(“/media/”)
Fileval=open('NextVal.txt','r')
Fileupdate=Fileval.read()
Fileval.close()
Fileupdate=int(Fileupdate)
Filect=Fileupdate/10000
Filect2=str(Filect)+“0009”
Filecount=int(Filect2)

当您谈到使用txt在程序之间传递信息时,我们首先需要知道您使用的是什么语言。 在我的Java和Python知识范围内,achi是可行的,尽管需要大量的信息


在python中,您可以使用它附带的库来读写txt和调度执行,您可以使用apscheduler。

这可以通过使用多处理库的简单客户机/服务器拓扑来实现。使用您的母亲/孩子术语:

server.py

from multiprocessing.connection import Listener

# client
def child(conn):
    while True:
        msg = conn.recv()
        # this just echos the value back, replace with your custom logic
        conn.send(msg)

# server
def mother(address):
    serv = Listener(address)
    while True:
        client = serv.accept()
        child(client)

mother(('', 5000))
from multiprocessing.connection import Client

c = Client(('localhost', 5000))

c.send('hello')
print('Got:', c.recv())

c.send({'a': 123})
print('Got:', c.recv())
client.py

from multiprocessing.connection import Listener

# client
def child(conn):
    while True:
        msg = conn.recv()
        # this just echos the value back, replace with your custom logic
        conn.send(msg)

# server
def mother(address):
    serv = Listener(address)
    while True:
        client = serv.accept()
        child(client)

mother(('', 5000))
from multiprocessing.connection import Client

c = Client(('localhost', 5000))

c.send('hello')
print('Got:', c.recv())

c.send({'a': 123})
print('Got:', c.recv())

$ python server.py
$ python client.py

伙计。添加一些换行符。阅读起来很痛苦。问题的标题是“Python”。另一种语言,人们可以为您提供更具体的答案,并利用您的知识来增强答案。因此,假设语言不同,除了本地tcp端口,一个人可以在不同的任务之间使用这种“所谓的多处理”交互通信行为吗?因为这是我一直使用的(对于非tcp连接),在这种情况下,所提到的协议的技术名称是什么?