Wait until the process is completed

Asked by fordox

While True:

       ..... # some code

        if typei == '1':
            url = '/getsome' + rid
            args = ('C:\file.exe', '--silent' + url)
            popen = subprocess.Popen(args, stdout=subprocess.PIPE)
            popen.communicate()

       ..... # some code

        continue

How do I make a script waiting for 180 seconds after the start of the file?
If the process of the file.exe has not been completed - to return to the beginning of the cycle
If the process was complete - continue code execution

SIGALRM doesn't work in Windows :-(

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:

This question was reopened

Revision history for this message
Eugene S (shragovich) said :
#1

You probably should ask that question on StackOverflow or some other QA site as it's not a Sikuli specific question.

Revision history for this message
RaiMan (raimund-hocke) said :
#2

the docs for Jython subprocess.Popen
http://www.jython.org/docs/library/subprocess.html

there is Popen.poll() that can be used to check, wether the subprocess has terminated or not.
Furthermore it does not look like you want to use the output to STDOUT of the called program, so you should not specify the stdout parameter.

BTW: I would never name a variable the same as a used module or class.

while True:
  # ... some code
  if typei == '1':
    url = '/getsome' + rid
    args = ('C:\file.exe', '--silent' + url)
    myApp = subprocess.Popen(args)
    hasTerminated = False
    shouldStop = time.time() + 180
    while not hasTerminated:
      ret = myApp.poll()
      if ret:
        hasTerminated = True
        break
      if time.time() > shouldStop:
        myApp.terminate()
        break
  if not hasTerminated:
    continue

# code in case it terminated within 180 secs

Revision history for this message
fordox (neveroid) said :
#3

Thanks RaiMan, that solved my question.

Revision history for this message
fordox (neveroid) said :
#4

there is an error:

[error] AttributeError ( 'Popen' object has no attribute 'terminate' )

on myApp.terminate()

Revision history for this message
RaiMan (raimund-hocke) said :
#5

If you are working with the latest build of SikuliX 1.1.0, it should be there.

… if not (using Jython < 2.7) you have to get the pid using Popen.pid and issue a kill command.

Revision history for this message
fordox (neveroid) said :
#6

In latest version of Sikuli unfortunately does not work properly module "Urlib"

[error] java.lang.IllegalStateException ( java.lang.IllegalStateException: failed to create a child event loop )
[error] --- Traceback --- error source first line: module ( function ) statement 26: urllib ( <module> )

So I'm using version 1.0.1.
Please give an example, how I can using Popen.pid and issue a kill command? :-) if not more difficult.

Revision history for this message
Best RaiMan (raimund-hocke) said :
#7

tried urllib2 ??

about Popen.pid see the mentioned doc.

about how to terminate a process using its pid form command line see the Windows docs.

Revision history for this message
fordox (neveroid) said :
#8

Thanks RaiMan, that solved my question.