Devyn Collier Johnson
Devyn Collier Johnson

Reputation: 4292

Starting a Python script from within another - odd behavior

Through a command-line (/bin/sh) on a Ubuntu system, I executed a Python3 script that uses multiprocessing.Process() to start another Python3 script. I got the error message below:

collier@Nacho-Laptop:/media/collier/BETABOTS/Neobot$ ./Betabot #THE SECOND SCRIPT NEVER EXECUTES
/bin/sh: 1: Syntax error: "(" unexpected (expecting "}")
Traceback (most recent call last):
  File "./Betabot", line 26, in <module>
    JOB_CONFIG = multiprocessing.Process(os.system('./conf/set_data.py3'))
  File "/usr/lib/python3.3/multiprocessing/process.py", line 72, in __init__
    assert group is None, 'group argument must be None for now'
AssertionError: group argument must be None for now

#TESTING THE SECOND SCRIPT BY ITSELF IN TWO WAYS (both work)
collier@Nacho-Laptop:/media/collier/BETABOTS/Neobot$ python3 -c "import os; os.system('./conf/set_data.py3')" #WORKS
collier@Nacho-Laptop:/media/collier/BETABOTS/Neobot$ ./conf/set_data.py3 #WORKS

The question is - Why is this not working? It should start the second script and both continue executing with out issues.

I made edits to the code trying to solve the issue. The error is now on line 13. The same error occurs on line 12 "JOB_CONFIG = multiprocessing.Process(os.system('date')); JOB_CONFIG.start()" that I used as a testing line. I changed line 12 to be "os.system('date')" and that works, so the error lies in the multiprocessing command.

#!/usr/bin/env python3
import os, subprocess, multiprocessing
def write2file(openfile, WRITE):
    with open(openfile, 'w') as file:
        file.write(str(WRITE))
writetofile = writefile = filewrite = writer = filewriter = write2file
global BOTNAME, BOTINIT
BOTNAME = subprocess.getoutput('cat ./conf/startup.xml | grep -E -i -e \'<property name=\"botname\" value\' | ssed -r -e "s|<property name=\"botname\" value=\"(.*)\"/>|\1|gI"')
BOTINIT = os.getpid()

###Setup science information under ./mem/###
JOB_CONFIG = multiprocessing.Process(os.system('date')); JOB_CONFIG.start()
JOB_CONFIG = multiprocessing.Process(os.system('./conf/set_data.py3')); JOB_CONFIG.start()
###START###
write2file('./mem/BOTINIT_PID', BOTINIT); write2file('./mem/tty', os.ctermid()); write2file('./mem/SERVER_PID', BOTINIT)
JOB_EMOTION = multiprocessing.Process(os.system('./lib/emoterm -T Emotion -e ./lib/Emotion_System')); JOB_EMOTION.start()
JOB_SENSORY = multiprocessing.Process(os.system('./lib/Sensory_System')); JOB_SENSORY.start()
print(BOTNAME + ' is starting'); JOB_CONFIG.join()
try:
    os.system('./lib/neoterm -T' + BOTNAME + ' -e ./lib/beta_engine')
except:
    print('There seems to be an error.'); JOB_EMOTION.join(); JOB_SENSORY.join(); exit()
JOB_EMOTION.join(); JOB_SENSORY.join(); exit()

Upvotes: 1

Views: 240

Answers (1)

Devyn Collier Johnson
Devyn Collier Johnson

Reputation: 4292

When starting a Python3 script from a Python3 script that is to be run while the main script continues, a command like this must be done:

JOB_CONFIG = subprocess.Popen([sys.executable, './conf/set_data.py3'])

The filename string is the script. This is save to a variable to allow me to manipulate the process later. For instance, I could use the command "JOB_CONFIG.wait()" when the main script should wait for the other script.

As for that hashpling in the first line of the error message, that is due to a syntax error in the first subprocess command used.

Upvotes: 1

Related Questions