How i can paused/continued python behave script

I am trying to implement message testing in the messenger. To do this, I run the script_1 in which I send a message. Then in this script I want to call script_2. Script_2 will allow me to log into my application as another user and read this message. But at the same time, I need the script_1 to pause while script_2 is running. How can I do this in python or behave?

I am writing in script_2.

p = subprocess.Popen("gnome-terminal -- behave features/{0}".format(feature_name), shell=True)
    p.send_signal(signal=signal.pause())

and indeed then script_1 is paused. But I do not know. how to continue script_1. when script_2 completes

Upvotes: 0

Views: 829

Answers (2)

urban
urban

Reputation: 5682

A bit late I know but the following demonstrates what @Sima is saying p.send_signal(signal.SIGCONT) would start a paused process.

UPDATE: based on the comments I understand that you need to make script_1 (msg send) wait for script_2 (login) to complete before continuing. Process 1 below is the python script that is going to send the message while process 2 is the login process:

import subprocess
import signal
import time

print("Starting")
p = subprocess.Popen("""
for i in `seq 1 12`;
do
    echo $i;
    sleep 1;
done
""", shell=True)

# Your script_1 controlling script_2 here
print("Process 1: Sleeping")
time.sleep(3)

print("Process 2: Pausing")
p.send_signal(signal.SIGTSTP)

print("Process 1: Sleeping")
time.sleep(3)

print("Process 2: Starting (continue)")
p.send_signal(signal.SIGCONT)

# Your script_1 waiting for script_2 to complete in order to continue
print("Process 1: Waiting on 2 to finish")
time.sleep(3)

# Code from https://stackoverflow.com/q/36596354/3727050
while p.poll() is None:
    # Process hasn't exited yet, let's wait some
    time.sleep(0.5)
    print("Process 1: still waiting...")

print("Process 1 got process 2 exit code: {}".format(p.returncode))
print("Here only Process 1 is running")

The output is:

$ python ~/tmp/tests.py
Starting
Process 1: Sleeping
1
2
3
Process 2: Pausing
Process 1: Sleeping
Process 2: Starting (continue)
Process 1: Waiting on 2 to finish
4
5
6
7
Process 1: still waiting...
Process 1: still waiting...
8
Process 1: still waiting...
Process 1: still waiting...
9
Process 1: still waiting...
Process 1: still waiting...
10
Process 1: still waiting...
Process 1: still waiting...
11
Process 1: still waiting...
Process 1: still waiting...
12
Process 1: still waiting...
Process 1: still waiting...
Process 1: still waiting...
Process 1 got process 2 exit code: 0
Here only Process 1 is running

Note that from the exit-code of script 2 you should be able to detect if login was successful. A return code of 0 is success, anything else is failure

Upvotes: 1

Sima
Sima

Reputation: 122

You can send continue signal when you want, for example in end of script_2.

p.send_signal(signal.SIGSTOP)
"""do what you want"""
p.send_signal(signal.SIGCONT)

Upvotes: 1

Related Questions