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:

2 Likes

Hi Robot Forum, I used the fork of the AsyncLibrary and I have the error in VSCode, that the Library import cannot be resolved.

Library AsyncLibrary

Unresolved library: AsyncLibrary.
Error generating libspec:
Importing library 'AsyncLibrary' failed: ModuleNotFoundError: No module named 'AsyncLibrary'
Consider adding the needed paths to the "robot.pythonpath" setting
and calling the "Robot Framework: Clear caches and restart" action.robotframework

I already updated the issue in the forks repo:

Unresolved library: AsyncLibrary. · Issue #9 · rlehfeld/robotframework-async-keyword (github.com)

Does anyone have an idea? It has something to do with arguments of the Library, which are missing.

Error generating libspec:
Initializing library ‘AsyncLibrary’ with no arguments failed: RobotNotRunningError: Cannot access execution context
robotframework

That feels like the issue is triggered by the LSP Server and implementation not taking that into account and by a brief look, this seems to be the case.

When LSP tries to get a list of keywords from the Library, it calls the library’s constructor. If any part of the call chain for that constructor calls any robot framework API’s that requires RF to be running, this will cause that constructor call to fail for that “RobotNotRunningError”.

In this case, AsyncLibrary’s constructor initializes self._postpone at robotframework-async-keyword/AsyncLibrary/robot_async.py at main · rlehfeld/robotframework-async-keyword · GitHub

On Postpone constructor at robotframework-async-keyword/AsyncLibrary/robot_async.py at main · rlehfeld/robotframework-async-keyword · GitHub - there’s a call to BuiltIn() which will throw that error if the execution was not started from RF.

PS. This error does not affect actual execution of your test assets, its only triggered in your IDE/Editor utilizing the LSP …

1 Like

Thank you for the explanation.
It somehow makes calling library keywords not working in VSCode.
Which implantation of get_context() can I use to fix the error?

That should fix the issue…

2 Likes

Thank you very much!