Search code examples
pythonc++subprocesssignalssleep

Does time.sleep stop subprocess in python?


We have C++ binary which will watch for some directories, and if any new file is added to those directories, C++ binary parses the file, creates new file and place it in some other directory.

We need to test the processed file. We used python to test this. Our python test case does below

  • Start the C++ binary in sub process.
  • Add some files in to a directory.
  • Wait for some time to give some processing time to C++ binary.
  • test the processed file content.

Below is the pseudo code I used.

file = 'SomeFile.txt'
prc = subprocess.Popen([cpppath, "-v", "-c", cfg], stdout=file)
# Add some files in to a directory
time.sleep(5) # To give time to C++ binary
# Test the content

But the issue I observed is, time.sleep() also stops C++ binary. I came to know this because we have a cron thread in C++ binary which will print I am Alive every second. This is getting printed till the time we call timer.sleep() in python. Once the sleep ends, Python test started testing the content. But as C++ binary didn't get chance to process the file in watch directory, test is failing.

My manager suggested to use signals. Python test case will create the files in watch directory and waits for sigusr1 and once the signal comes, test will proceed. C++ binary process the input files, place the processed file in a directory and signals parent process which is python test. But even for this, python test needs to use sleep() or pause() which will be the same issue again.

Can anyone please let me know if there is any way to solve the issue.


Solution

  • No, calling time.sleep() in Python does not affect the binary started with subprocess.Popen(). There must be some other reason the test doesn't work as expected.