Search code examples
pythonlistflask

Sharing variables between two seperatly running Python scripts


I am trying to get a Python 3.11 script with a flask app talking to a fully separate Python 2.7 script that is controlling a robot. The Python 3.11 file creates angles that the robot arm receives.

The current problem is that I haven't found a way to get a list of angles from one program to the next. Also, the sending of the angles has to be very fast because they are being created from a live video. The function to send the angles currently looks like this:

def sendAngles(videomodus,liste): # videomodus is a variable to make clear if its a list of lists with angles or just a list with angles
   # this function gets called every frame

I have already tried using socket and writing to a file, but in both situation, both programs get stuck.


Solution

  • I now use pipe for sending the list between the two programs.

    Sender:

    import json 
    import os
    
    check_path = '/home/alex/robbi-master/robbi/pipes/check'
    listen_path = '/home/alex/robbi-master/robbi/pipes/liste'
    
    if not os.path.exists(check_path):
        os.mkfifo(check_path)
    if not os.path.exists(listen_path):
        os.mkfifo(listen_path)
    
    def sendwinkel(videoModus, liste):
        if not videoModus:
            with open(listen_path, 'w') as liste_fifo:
                data = json.dumps(liste)
                liste_fifo.write(data)
            with open(check_path, 'r') as check:
                check.read()
        elif videoModus:
            a = 0
            while a < liste.length:
                with open(listen_path, 'w') as liste_fifo:
                    data = json.dumps(liste[a])
                    liste_fifo.write(data)
                with open(check_path, 'r') as check:
                    check.read()
                a += 1
    
    

    Receiver (ros script):

    def go_to_joint_state(self):
            
        joint_goal  = self.group.get_current_joint_values()
    
        with open(listen_path, 'r') as listen_fifo:
            data = listen_fifo.read()
            joint_goal = json.loads(data)
            #self.group.go(joint_goal, wait=True)
            print(joint_goal)
        with open(check_path, 'w') as check:
            check.write('ok')
        self.group.stop()