使用Python的RobotFramework';s异步

使用Python的RobotFramework';s异步,python,robotframework,python-asyncio,python-3.6,Python,Robotframework,Python Asyncio,Python 3.6,我正在尝试使用Python3.6的asyncio运行RobotFramework 相关Python代码如下所示: """ SampleProtTest.py """ import asyncio import threading class SubscriberClientProtocol(asyncio.Protocol): """ Generic, Asynchronous protocol that allows sending using a synchronous

我正在尝试使用Python3.6的asyncio运行RobotFramework

相关Python代码如下所示:

""" SampleProtTest.py """

import asyncio
import threading

class SubscriberClientProtocol(asyncio.Protocol):
    """
    Generic, Asynchronous protocol that allows sending using a synchronous accessible queue
    Based on http://stackoverflow.com/a/30940625/4150378
    """
    def __init__(self, loop):
        self.loop = loop

    """ Functions follow for reading... """


class PropHost:
    def __init__(self, ip: str, port: int = 50505) -> None:
        self.loop = asyncio.get_event_loop()
        self.__coro = self.loop.create_connection(lambda: SubscriberClientProtocol(self.loop), ip, port)
        _, self.__proto = self.loop.run_until_complete(self.__coro)
        # run the asyncio-loop in background thread
        threading.Thread(target=self.runfunc).start()

    def runfunc(self) -> None:
        self.loop.run_forever()

    def dosomething(self):
        print("I'm doing something")


class SampleProtTest(object):
    def __init__(self, ip='127.0.0.1', port=8000):
        self._myhost = PropHost(ip, port)

    def do_something(self):
        self._myhost.dosomething()

if __name__=="__main__":
    tester = SampleProtTest()
    tester.do_something()
如果我在python中运行此文件,它将按预期打印:

I'm doing something
为了在Robot Framework中运行代码,我编写了以下.Robot文件:

*** Settings ***
Documentation     Just A Sample
Library           SampleProtTest.py
*** Test Cases ***
Do anything
    do_something
但如果运行此.robot文件,则会出现以下错误:

Initializing test library 'SampleProtTest' with no arguments failed: This event loop is already running
Traceback (most recent call last):
  File "SampleProtTest.py", line 34, in __init__
    self._myhost = PropHost(ip, port)
  File "SampleProtTest.py", line 21, in __init__
    _, self.__proto = self.loop.run_until_complete(self.__coro)
  File "appdata\local\programs\python\python36\lib\asyncio\base_events.py", line 454, in run_until_complete
    self.run_forever()
  File "appdata\local\programs\python\python36\lib\asyncio\base_events.py", line 408, in run_forever
    raise RuntimeError('This event loop is already running')
有人能给我解释一下为什么或者怎样才能避开这个问题吗

多谢各位

编辑

由于@Dandekar,我添加了一些调试输出,请参见上面的代码,并从robot获得以下输出:

- Loop until complete...
- Starting Thread...
- Running in thread...
==============================================================================
Sample :: Just A Sample                                                       
==============================================================================
Do anything                                                           - Loop until complete...
| FAIL |
Initializing test library 'SampleProtTest' with no arguments failed: This event loop is already running
Traceback (most recent call last):
  File "C:\share\TestAutomation\SampleProtTest.py", line 42, in __init__
    self._myhost = PropHost(ip, port)
  File "C:\share\TestAutomation\SampleProtTest.py", line 24, in __init__
    _, self.__proto = self.loop.run_until_complete(self.__coro)
  File "c:\users\muechr\appdata\local\programs\python\python36\lib\asyncio\base_events.py", line 454, in run_until_complete
    self.run_forever()
  File "c:\users\muechr\appdata\local\programs\python\python36\lib\asyncio\base_events.py", line 408, in run_forever
    raise RuntimeError('This event loop is already running')
------------------------------------------------------------------------------
Sample :: Just A Sample                                               | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
Output:  C:\share\TestAutomation\results\output.xml
Log:     C:\share\TestAutomation\results\log.html
Report:  C:\share\TestAutomation\results\report.html
在我看来,问题在于它在测试用例之前已经启动了线程。奇怪的是,如果我拆下这条线

_, self.__proto = self.loop.run_until_complete(self.__coro)

它似乎贯穿始终-但我无法解释为什么。。。但这不是一个实用的解决方案,因为我无法访问像这样的proto…

编辑:注释掉代码开始时运行的部分

# if __name__=="__main__":
#    tester = SampleProtTest()
#    tester.do_something()
当您在robot框架中导入脚本时,该部分就会运行(导致端口被占用)

另外:如果您只是尝试异步运行关键字,那么有一个库可以做到这一点(尽管我自己没有尝试过)


非常感谢您的建议,但对于我来说,最好只使用“常规”robotframework,以便能够从IP连续读取Python并请求robotframework发送/分析。由于我的Python版本,我无法访问asyncio,但从您的代码中,我会说,您可以简单地随机选择端口,看看是否有效<代码>来自随机导入选项,然后在设置端口的位置,
端口=选项(范围(80009000))
。如果这样做有效,那么您可以增强代码,使端口真正随机。事实上,我想我已经发现了问题。注释掉代码的自动运行部分<代码>如果uu name uuu==…请原谅,我完全监督了您的帖子-非常感谢!不幸的是,这没有帮助。在Robot框架中,
if\uu name\uuu…
部分未执行。如果我从robot.api导入记录器添加
。。。如果uuuu name uuuuu==“uuuuuu main uuuuuuuu”:logger.console(“-Hello world:-”)”)我在执行Robot框架时没有看到“Hello world”消息。因为我没有访问或添加asyncio的能力,所以我无法在工作中复制此消息。将尝试在家中复制,并查看错误是什么。但是线程确实在测试运行之前启动并占用了端口,所以至少问题已经被识别出来了。