1

There is a socket server that is working properly, they asked me to make a minimal interface to it, I did it using PyQt5 there are 2 buttons in it: Starting the server and Shutting down the server, implemented via subprocess as follows:

Launch:

def start_serv(self):
    self.process = subprocess.Popen('python server.py', shell=True)
    logging.info('#' * 80)
    logging.debug(f'Process PID: {self.process.pid}')
    logging.debug(f'Child process return code: {self.process.returncode}')
    self.mb('The server is running!') # sending a message to a dialog box

Shutdown:

def stop_serv(self):
    try:
        proc = self.process
        proc.send_signal(signal.SIGTERM)
        while True:
            ex_code = proc.poll()
            if ex_code == None or ex_code == '1':
                proc.wait()
                continue
            else:
                break
        logging.debug(f'Child process exit code: {proc.poll()}')
        logging.debug('The server is stopped!')
        logging.info('#' * 80)
        self.mb('The server is stopped!') # sending a message to a dialog box
    except Exception as e:
        logging.warning(f'Error button: {e}')
        self.mb('The server is not running yet!') # sending a message to a dialog box

Everything works fine in the IDE (PyCharm). But after the build using auto-py-to-exe, when you click on the start button, a process with a new PID is created, but the server does not start. The file paths were not changed during the build.

Please help me figure out where and what edits I need to make to make it work.

P.S.: Everything is done in Python 3.8.2 (customer's requirement)

I thought it was about the path to the script file, I tried different options:

self.path_server = Path(Path.cwd(), 'server.py')
self.process = subprocess.Popen(f'python {self.path_server}', shell=True)

or:

cwd = os.path.dirname(os.path.abspath(__file__))
self.process = subprocess.Popen('python server.py', shell=True, cwd=cwd)

I haven't found any other options for problems yet.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Pavel_G
  • 11
  • 3
  • _Why_ would you use `shell=True` here? It makes you get worse error messages and can break shutdown logic, because you get the PID of the shell, not the PID of your server; and if starting the child-process interpreter fails, you only find out indirectly by the shell returning an exit status indicating an error, instead of getting a direct `errno` return value to the relevant syscall. Use `['python', 'server.py']` without `shell=True`, expanding both paths (including the interpreter path) to be more explicit as and where appropriate. – Charles Duffy Jun 27 '23 at 12:14
  • Also, note that because you're running `python`, _the customer needs to have Python installed_. "auto-py-to-exe" won't paper over that. – Charles Duffy Jun 27 '23 at 12:16
  • (And beyond that: _Capture the error message!_ -- if you don't show us the error message, or worse, if you haven't even seen it yourself, you're running in the dark; the code in the question isn't a [mre] insofar as it currently isn't something we can run ourselves to see the same problem; worse, because you haven't shown us the error you get, we wouldn't know the problem if we _did_ reproduce it). – Charles Duffy Jun 27 '23 at 12:17
  • Alternately, you might consider using your py-to-exe tools on _both_ the GUI and the server it starts, and having the GUI start the exe for the server, not start `python`. – Charles Duffy Jun 27 '23 at 12:18
  • @CharlesDuffy You are right, the build does not start on the user's machine. Tell me how to solve this problem? P.S.: I figured out how to start the server. – Pavel_G Jun 28 '23 at 09:25

0 Answers0