Search code examples
pythonprocessrobotframeworkpid

get_process_id() from robot Process() library. "No active process"


I am trying to use the Process() robot framework library to launch and track processes. https://robot-framework.readthedocs.io/en/v3.0.3/_modules/robot/libraries/Process.html

After I launch my process I am unable to use the get_process_id() method. I wrote a simple example using notepad.exe below

path = "C:\\WINDOWS\\system32"
Process().start_process('notepad.exe',shell=False, cwd=path)
var = Process().get_process_id()
BuiltIn().log_to_console(var)

This gives me the error of "No active process."

Alternatively, using handles as explained in the documentation

path = "C:\\WINDOWS\\system32"
handle = Process().start_process('notepad.exe',shell=False,cwd=path)
var = Process().get_process_id(handle)
BuiltIn().log_to_console(var)

I get the error "Non-existing index or alias '1'."


Solution

  • When you do Process().get_process_id(), you are creating a new instance of the library. This instance doesn't know about any processes started by the previous instance of the library.

    You need to get a single instance of the library, and use that consistently.

    processLib = Process()
    processLib.start_process(...)
    var = processLib.get_process_id()
    

    The best thing to do is try to get a reference to an existing process library using BuiltIn().get_library_instance, and only create a new one if it doesn't exist.