0

I am trying to launch a Python program as a subprocess for each web connection through a Flask server.

In fact, I am trying to launch a process to run AutoGPT. It works but each launched AutoGPT process seems to have the same memory space.

from subprocess import Popen, call, PIPE
import sys
import re
import io

class AutoGPT:
    cache_running_process = []
    def __getCache__(self, sid):
        for e in AutoGPT.cache_running_process: # https://stackoverflow.com/a/13054570
            if e.sid == sid:
                return e
        return None

    def __init__(self, socketio, sid):
        self.socketio = socketio
        cache = self.__getCache__(sid)
        if cache is not None:
            self.sid = cache.sid
            self.proc = cache.proc
        else:
            self.proc = Popen(["./run.sh"], stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd="./Auto-GPT", start_new_session=True)
            self.sid = sid
            print('current sid :'+sid)
            self.cache_running_process.append(self)

    def check_tag_at_the_end(self, line_target):
        line_splitted = line_target.split()
        if len(line_splitted) < 2:
            return None
        return re.search("\: \]$", line_splitted[-2] + " " + line_splitted[-1])

    def read_until_it_finishes(self):
        line = ""
        colorama = False
        while self.proc.poll() is None:
            char =  self.proc.stdout.read(1) # https://stackoverflow.com/a/63137801/10294022
            char_utf8 = char.decode('utf-8', 'ignore')
            line += char_utf8
            if self.check_tag_at_the_end(line) is not None:
                break
            if colorama == True and char == bytes('m', 'utf-8'):
                # We are finishing with ANSI characters for color text in terminal https://github.com/tartley/colorama#recognised-ansi-sequences
                colorama = False
            else:
                if colorama == False and char != bytes('\x1b', 'utf-8'):
                    if char == bytes('\n', 'utf-8'):
                        self.socketio.emit('my_response', 
                            {'data': "<br />"}, namespace='/test')
                    else:
                        self.socketio.emit('my_response', 
                            {'data': char_utf8}, namespace='/test')
                else:
                    # We are starting with ANSI characters for color text in terminal
                    colorama = True

    def write(self, message):
        self.proc.stdin.write(bytes(message + "\n", 'utf-8'))
        self.proc.stdin.flush()
        self.read_until_it_finishes()

    def close(self):
        self.proc.terminate()
        cache = self.__getCache__(self.sid)
        index = self.cache_running_process.index(cache)
        del self.cache_running_process[index]

Then, when I give the name TOTO to the first process and TITI to the second, the first process, unfortunately, gets the TOTO name!

What am I doing wrong?

tripleee
  • 175,061
  • 34
  • 275
  • 318
Alex A.
  • 9
  • 1
  • Does it use a shared memory segment or something weird like that? – tadman May 05 '23 at 02:19
  • How do you establish that they use the "same memory space"? A proper [mre] of what I think you seem to be asking does not seem to reproduce the problem: https://ideone.com/KOYsND – tripleee May 05 '23 at 04:37
  • Hi, I think it was an error from me. In fact there were 2 different process. It was the stdout of the two process which were mixed. – Alex A. May 05 '23 at 21:34

0 Answers0