If my Python Script gives different results overtime, how can I get those outcomes to UiPath without restarting the script (and therefore ensure to receive the info over the process)?

Hi team,

I hope everything is fine!
So basically what I intend to do is the following: I have a certain python script that gives different results across time. I was able to bring the script to UIPath by using Load Python Script > Invoke Python Method > Get Python Method, all inside a Python Scope activity (until here everything is fine).
The python script is made in a way that gives different outcomes overtime. These outcomes are used as directions/orders so that UiPath continue the process according those instructions.
The main issue here is that I am not being able to get the outcome of the python script overtime. If I use the regular sequence (Load Python Script > Invoke Python Method > Get Python Method, all inside a Python Scope activity), the script will always restart, meaning that UiPath will only get a fraction (the first outcome) of the python script.

Therefore, my question is; how can we get information from python over the process without restarting the script everytime?

PS: Imagine a python script that gives “1” and after one second it gives “2”; how can i get these two results in UiPath? If I use the “Load Python Script>…”, I will always get the number 1.

If I understood you correctly - what you’re trying to achieve is periodically run the Python script and get its outcome. You’re using the Python activities to make this happen and you’re executing the script. After the execution it leaves the Python Scope, continues the robot and eventually ends. You actually want to execute the Python script again, get its output and process it again.

In order to achieve this what you’re able to do is put the activities you would like to repeat in a While loop. Set the while loop’s condition to True. This will keep your robot in a loop until the condition True evaluates to False. This will never happen. Inside the loop, at the very end, place a Delay activity for the amount of seconds which suits your business logic. What I mean by this is - if you expect a new output from your Python script every minute, set the delay to 30 seconds and not 2 seconds. This will prevent the activities inside of the loop to excessively repeat. Note that just setting the While’s condition to True is bad practice. There needs to be some sort of way to gracefully close your robot. What the best way is totally depends on your robot’s setup (attended/unattended, orchestrator/local, dispatcher/performer, etc.)

Hi,
Many thanks for your reply!
The main problem here is that the python script change overtime and in order to get to the second outcome (third, fourth, etc) it needs to pass to the first. This means that I cannot load the script multiple times as UiPath will always get only the first outcome.
What I am looking for is if there is a way to get the python script running “in the background”, meaning that whenever UiPath needs to check the outcome of the script it will not load the python script every time. It would have some “checkpoints” that would check if the result from the script is the same as previously or not and continue according that.

PS: I assume to do that (if possible), I would have to insert a flowchart inside a Python Scope. Then, I would have something like the following picture (this is just an example and is incorrect as it gives always the first outcome of the python script; just to better explain what I intend to do):

Below you can also find the python script:

def Example():

x=1

while x<5:
    r=1
    x=x+1
    return(r)

r=2
return(r)

I never tried this but you could return a generator as python object, loop over it in UiPath by calling its next method converting the result to VB object (and catching StopIteration properly).

class Example:
    def __init__(self):
        self._value = self.example()
    
    def next(self):
        return next(self._value)
    
    def example(self):
        for _ in range(5):
            yield 1
        yield 2
  • Load the script (Load Python Script Activity)
  • Instanciate the class as PyObject (Invoke Python Method Activity)
  • Loop over this PyObject .next() method (Invoke Python Method Activity)
    • Convert it as VB variable (Get Python Object Activity)
    • Process it
a = Example()
a.next()
a.next()
a.next()
a.next()
a.next()
# StopIteration here
a.next()

Example of implementation in UiPath (you’ll need to update the python path, I didn’t put any variable, shhhht)

Sequence.xaml (7.5 KB)

1 Like