How to run keywords asynchronously in robotframework

Tried with robotframework-asynclibrary
github-link GitHub - Chetic/robotframework-async: Generic Robot Framework library for asynchronous keyword execution
Followed instruction and getting error

*** Settings ***
Library     AsyncLibrary

*** Test Cases ***
Example Test
   ${handle}    async run    Task1
   ${handle2}      async run   Task2

    Log    Both tasks have finished

*** Keywords ***
Task1
    Log    Task 1 started
    Sleep    2

    Log    Task 1 finished

Task2
    Log    Task 2 started
    Sleep    3
    Log    Task 2 finished

error:

Example Test                                                          
Exception in thread Thread-1 (wrapped_f):
Traceback (most recent call last):                                                                             
  File "C:\Program Files\Python310\lib\threading.py", line 1009, in _bootstrap_inner                           
.Exception in thread Thread-2 (wrapped_f):                                                                     
Traceback (most recent call last):                                                                             
  File "C:\Program Files\Python310\lib\threading.py", line 1009, in _bootstrap_inner

Hi Harsha,

First time seeing that library but it looks useful, thanks for finding it :+1:

As I’ve not seen it before, I’m not an expert, however from a quick look at the library README and the code I thing I understand what is happening.

With threading you should wait for the thread to end before exiting the process that created that thread, in your case you are starting 2 threads ( async run) , logging some text to the log and exiting so the main process ( Example Test) is finishing before the threads ( Task1 and Task2) which would explain those errors.

Here’s a suggestion for a slightly modified test that should hopefully work better as it should wait for the threads to finish.

*** Settings ***
Library     AsyncLibrary

*** Test Cases ***
Example Test
   ${handle}    async run    Task1
   ${handle2}      async run   Task2

    Log    Both tasks have started

    ${return_value1}    async get    ${handle}
    ${return_value2}    async get    ${handle2}

    Log    Both tasks have finished

*** Keywords ***
Task1
    Log    Task 1 started
    Sleep    2

    Log    Task 1 finished

Task2
    Log    Task 2 started
    Sleep    3
    Log    Task 2 finished

Hope that helps,

Dave.

Hi Dave By using the above code which you have provided the execution is not terminating and i have used log to console instead of log but not able to see the log statement. and i have to terminate the process manually to stop the execution.

Dave can you please help to resolve this issue

Hi Harsha,

What did you get in the log? Can you show the log? (it’s really hard to troubleshoot without the logs)

I expect the version i gave you would take ~3 seconds to finish, how long did you wait?

Dave.

Hi dave reports and logs are not generating because execution not completed
i am uploading the console output

for 3seconds i have used the wait

output

Hi Harsha,

Ok I setup a VM with a minimal Debian 11 and installed pip, then ran robotframework-async copied my example and ran it to reproduce your issue.

Here’s what i got:

dave@Harsha:~/tmp$ robot --version
Robot Framework 6.0.2 (Python 3.9.2 on linux)
dave@Harsha:~/tmp$ 
dave@Harsha:~/tmp$ robot Harsha-AsyncLibrary.robot 
==============================================================================
Harsha-AsyncLibrary                                                           
==============================================================================
Example Test                                                          Both tasks have started
.Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
.Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
.Both tasks have started
.^CSecond signal will force exit.
Example Test                                                          | FAIL |
Execution terminated by signal
------------------------------------------------------------------------------
Harsha-AsyncLibrary                                                   | FAIL |
1 test, 0 passed, 1 failed
==============================================================================
Output:  /home/dave/tmp/output.xml
Log:     /home/dave/tmp/log.html
Report:  /home/dave/tmp/report.html
dave@Harsha:~/tmp$ 

Drillling down into the logs:

I can see that the errors you are getting are coming from the async run keyword.

So perhaps this library doesn’t work with newer versions of Python? It seems the last time it was updated was ~5 years ago for Python 3.7

You could try raising an issue and see if the author of this library responds

Feel free to link to this thread or put my verification in the issue.

Sorry i can’t help more than that, I don’t have time to take on maintaining another project.

Dave.

Thank you very much dave for your time

1 Like

Hi @Harsha.
I’ve developed robotframework-gevent library for just that.

Give it a try :relaxed:

1 Like