Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing values from one Python program to another

Tags:

python

ipc

Is it possible -- other than by using something like a .txt/dummy file -- to pass a value from one program to another?

I have a program that uses a .txt file to pass a starting value to another program. I update the value in the file in between starting the program each time I run it (ten times, essentially simultaneously). Doing this is fine, but I would like to have the 'child' program report back to the 'mother' program when it is finished, and also report back what files it found to download.

Is it possible to do this without using eleven files to do it (that's one for each instance of the 'child' to 'mother' reporting, and one file for the 'mother' to 'child')? I am talking about completely separate programs, not classes or functions or anything like that.

To operate efficently, and not be waiting around for hours for everything to complete, I need the 'child' program to run ten times and get things done MUCH faster. Thus I run the child program ten times and give each program a separate range to check through.

Both programs run fine, I but would like to get them to run/report back and forth with each other and hopefully not be using file 'transmission' to accomplish the task, especially on the child-mother side of the transferring of data.

'Mother' program...currently

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()

'Child' program

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()

I'm trying to convert the original VB6 program over to Linux and make it much easier to use at the same time. Hence the lack of .mainloop being missing. This was my first real attempt at anything in Python at all hence the lack of def or classes. I'm trying to come back and finish this up after 1.5 months of doing nothing with it mostly due to not knowing how to. In research a little while ago I found this is WAY over my head. I haven't ever did anything with threads/sockets/client/server interaction so I'm purely an idiot in this case. Google anything on it and I just get brought right back here to stackoverflow.

Yes, I want 10 running copies of the program at the same time, to save time. I could do without the gui interface if it was possible for the program to report back to 'mother' so the mother could print on the screen the current value that is being searched. As well as if the child could report back when its finished and if it had any file that it downloaded successfully(versus downloaded and then erased due to being to small). I would use the successful download information to update Highest.txt for the next time the program got ran.

I think this may clarify things MUCH better...that or I don't understand the nature of using server/client interaction:) Only reason time.sleep is in the program was due to try to make sure that the files could get written before the next instance of the child program got ran. I didn't know for sure what kind of timing issue I may run into so I included those lines for safety.

like image 602
confused Avatar asked Jan 12 '14 20:01

confused


2 Answers

This can be implemented using a simple client/server topology using the multiprocessing library. Using your mother/child terminology:

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))

client.py

from multiprocessing.connection import Client

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

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

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

Run with

$ python server.py
$ python client.py
like image 177
Stephen Diehl Avatar answered Oct 16 '22 07:10

Stephen Diehl


When you talk about using txt to pass information between programs, we first need to know what language you're using. Within my knowledge of Java and Python achi viable despite laborious depensendo the amount of information that wants to work.

In python, you can use the library that comes with it for reading and writing txt and schedule execution, you can use the apscheduler.

like image 28
Felipe Cabral Avatar answered Oct 16 '22 08:10

Felipe Cabral