# How to run keywords asynchronously in robotframework

**URL:** <https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308>\
**Category:** Robot Framework\
**Created:** [13 February 2023 13:44 UTC](https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308 "2023-02-13T13:44:34Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harsha](https://avatars.discourse-cdn.com/v4/letter/h/e274bd/32.png) [@Harsha](https://forum.robotframework.org/u/Harsha)\
**Post date:** [13 February 2023 13:44 UTC](https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308/1 "2023-02-13T13:44:34Z")

</div>

Tried with robotframework-asynclibrary  
github-link [GitHub - Chetic/robotframework-async: Generic Robot Framework library for asynchronous keyword execution](https://github.com/Chetic/robotframework-async)  
Followed instruction and getting error

```plaintext
***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:

```python
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

```

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [14 February 2023 00:53 UTC](https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308/2 "2023-02-14T00:53:22Z")

</div>

Hi Harsha,

First time seeing that library but it looks useful, thanks for finding it 👍

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.

```python
***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.

---

<div class="post-metadata">

**Author:** ![Harsha](https://avatars.discourse-cdn.com/v4/letter/h/e274bd/32.png) [@Harsha](https://forum.robotframework.org/u/Harsha)\
**Post date:** [14 February 2023 04:57 UTC](https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308/3 "2023-02-14T04:57:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Harsha](https://avatars.discourse-cdn.com/v4/letter/h/e274bd/32.png) [@Harsha](https://forum.robotframework.org/u/Harsha)\
**Post date:** [14 February 2023 09:59 UTC](https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308/4 "2023-02-14T09:59:10Z")

</div>

Dave can you please help to resolve this issue

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [14 February 2023 10:39 UTC](https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308/5 "2023-02-14T10:39:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Harsha](https://avatars.discourse-cdn.com/v4/letter/h/e274bd/32.png) [@Harsha](https://forum.robotframework.org/u/Harsha)\
**Post date:** [14 February 2023 10:56 UTC](https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308/6 "2023-02-14T10:56:06Z")

</div>

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

 ![Screenshot (216)](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/2/2d3e793c2bacc45f40b3b90054f17de24b699bf8.png)

---

<div class="post-metadata">

**Author:** ![Harsha](https://avatars.discourse-cdn.com/v4/letter/h/e274bd/32.png) [@Harsha](https://forum.robotframework.org/u/Harsha)\
**Post date:** [14 February 2023 10:58 UTC](https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308/7 "2023-02-14T10:58:43Z")

</div>

for 3seconds i have used the wait

---

<div class="post-metadata">

**Author:** ![Harsha](https://avatars.discourse-cdn.com/v4/letter/h/e274bd/32.png) [@Harsha](https://forum.robotframework.org/u/Harsha)\
**Post date:** [14 February 2023 10:59 UTC](https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308/8 "2023-02-14T10:59:16Z")

</div>

output

 ![Screenshot (216)](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/2/2d3e793c2bacc45f40b3b90054f17de24b699bf8.png)

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [14 February 2023 11:25 UTC](https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308/9 "2023-02-14T11:25:59Z")

</div>

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:

```python
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:

 ![Screenshot from 2023-02-14 21-15-29](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/c/cf2856e0bba9bce8322df5bf94a1fdc586e6731d.png)

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](https://github.com/Chetic/robotframework-async/issues) 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.

---

<div class="post-metadata">

**Author:** ![Harsha](https://avatars.discourse-cdn.com/v4/letter/h/e274bd/32.png) [@Harsha](https://forum.robotframework.org/u/Harsha)\
**Post date:** [14 February 2023 11:28 UTC](https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308/10 "2023-02-14T11:28:45Z")

</div>

Thank you very much dave for your time

---

<div class="post-metadata">

**Author:** ![eldaduzman](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/eldaduzman/32/1190_2.png) [@eldaduzman](https://forum.robotframework.org/u/eldaduzman)\
**Post date:** [15 February 2023 21:54 UTC](https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308/11 "2023-02-15T21:54:19Z")

</div>

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

> **[GitHub - eldaduzman/robotframework-gevent: Run robotframework keywords...](https://github.com/eldaduzman/robotframework-gevent)**
>
> Run robotframework keywords asynchronously with the power of gevent - GitHub - eldaduzman/robotframework-gevent: Run robotframework keywords asynchronously with the power of gevent

Give it a try 😌

---

<div class="post-metadata">

**Author:** ![Syman](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@Syman](https://forum.robotframework.org/u/Syman)\
**Post date:** [12 June 2024 11:29 UTC](https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308/12 "2024-06-12T11:29:18Z")

</div>

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)](https://github.com/rlehfeld/robotframework-async-keyword/issues/9)

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

---

<div class="post-metadata">

**Author:** ![rasjani](https://avatars.discourse-cdn.com/v4/letter/r/858c86/32.png) [@rasjani](https://forum.robotframework.org/u/rasjani)\
**Post date:** [12 June 2024 11:54 UTC](https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308/13 "2024-06-12T11:54:11Z")

</div>

> 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](https://github.com/rlehfeld/robotframework-async-keyword/blob/main/AsyncLibrary/robot_async.py#L369)

On Postpone constructor at [robotframework-async-keyword/AsyncLibrary/robot\_async.py at main · rlehfeld/robotframework-async-keyword · GitHub](https://github.com/rlehfeld/robotframework-async-keyword/blob/main/AsyncLibrary/robot_async.py#L44) - 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 …

---

<div class="post-metadata">

**Author:** ![Syman](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@Syman](https://forum.robotframework.org/u/Syman)\
**Post date:** [12 June 2024 12:29 UTC](https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308/14 "2024-06-12T12:29:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rasjani](https://avatars.discourse-cdn.com/v4/letter/r/858c86/32.png) [@rasjani](https://forum.robotframework.org/u/rasjani)\
**Post date:** [13 June 2024 09:03 UTC](https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308/15 "2024-06-13T09:03:14Z")

</div>

> <https://github.com/rlehfeld/robotframework-async-keyword/pull/11/files>
>
> Fixes #9

That _should_ fix the issue…

---

<div class="post-metadata">

**Author:** ![Syman](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@Syman](https://forum.robotframework.org/u/Syman)\
**Post date:** [19 June 2024 10:13 UTC](https://forum.robotframework.org/t/how-to-run-keywords-asynchronously-in-robotframework/5308/16 "2024-06-19T10:13:28Z")

</div>

Thank you very much!
