Forums

Run independent/separated Python script from another one.

Hello! Sorry for my English, I can make some mistakes. I read about long running processes on your site, but I can't include the whole code in one function, because it's a telegram bot. It needs to work without finishing as long as possible. So, I used your script with sockets, but I don't know how to run child processes (using Popen or system) from the script so that after finishing it my child processes are still running. Thank you P.S. I used os.system(python3.5 /.../myscript1.py&), Popen(.., shell = True), but after finishing the script, myscript1 also finishes.

You should actually run the long running processes check to see if there is another process of yours already running (if so, then exit immediately). And if no processes are running, then you can just run your code (which I assume loops indefinitely).

But my long running scripts will restart every time (every hour), because my script-checker restarts every hour! If I close process, which opens another processes, all processes will be closed. I don't know how to avoid that..

The code on the long-running task help page is designed to be the main file of your script; all it does is start up, check if there's already a process running, and exit if there is. So if you use it as documented, all that will normally happen is that every hour, your process will start, see that there's an existing one running, and immediately exit.

So, for a telegram bot using Telepot, you'll normally have with a bunch of handlers for different message types, and then code to start the loop like this:

bot = ChatBox(TOKEN, OWNER_ID)
MessageLoop(bot).run_as_thread()
print('Listening ...')

while 1:
    time.sleep(10)

...where ChatBot is your bot's main class. To make that work as a long-running task, just put that code into a function of its own:

def start_message_loop
    bot = ChatBox(TOKEN, OWNER_ID)
    MessageLoop(bot).run_as_thread()
    print('Listening ...')

    while 1:
        time.sleep(10)

...and then call start_message_loop instead of my_long_running_process in code based on the start script from the help page.