Search code examples
pythonbatch-filesikuli

How to run a batch file in background from Sikuli?


How to run a batch file in background from Sikuli?

Environment: WinXP, Python 2.7, Sikuli r930.

Let’s assume I have a simple batch file startNotepad.bat which contains 1 line:

START notepad

I start batch file using os.system method from Python script:

import os
os.system("startNotepad.bat")
os.system("d:\\newDir\\startNotepad.bat")
print("hello notepad")#displayed while 2 instances of notepad are running

When I start it from Python script it behaves like I want it to – notepad windows are opened in background, Python script continues after calling batch files. I also start batch file using os.system in Sikuli script and here comes trouble. Sikuli script looks like this:

import os
os.system("d:\\newDir\\startNotepad.bat")
print("hello notepad")#displayed only after notepad is closed

When I start it from Sikuli IDE there are errors:

[info] Sikuli vision engine loaded.
[info] Windows utilities loaded.
[info] VDictProxy loaded.
[error] Stopped
[error] Linia 2 zawiera blad
[error] Informacja o bledzie: Traceback (most recent call last):
 File "C:\DOCUME~1\*********\LOCALS~1\Temp\sikuli-tmp2989271839024887193.py", line 2, in 
 os.system("d:\\newDir\\startNotepad.bat")
 File "C:\Program Files\Sikuli X\sikuli-script.jar\Lib\subprocess.py", line 456, in call
File "C:\Program Files\Sikuli X\sikuli-script.jar\Lib\subprocess.py", line 751, in __init__
 File "C:\Program Files\Sikuli X\sikuli-script.jar\Lib\subprocess.py", line 1236, in _execute_child
TypeError: unsupported operand type(s) for +: 'NoneType' and 'list'

Ok,it is not a big deal because I use executable Sikuli script anyway. However, in case of non gui mode it doesn’t behave like intended – Sikuli script waits until notepad is closed.

Why os.system works differently in case of pure Python and Sikuli? I guess this is because Sikuli has its own Python interpreter. Can behavior be configured somehow? Is there any workaround for that?


Solution

  • The behaviour won’t be the same as python as the runtime for Sikuli is Java. Sikuli uses Jython, and jython is Java written using the python syntax.

    According to the documentation of os.system(command) it blocks to return the return code form the spawned sub shell. (http://docs.python.org/2/library/os.html#os.system)

    It is possible in python and Java the default shell states might be different and one returns when the shell command is complete and the other waits for the all the handles to close.

    Ideally what you want is a non-blocking subprocess.

    You can ether use pythons subprocess.popen or Sikuli’s App.open() to achieve this