在机器人框架中使用 python 的多线程,涉及嵌套函数调用

Multi-threading using python in robot framework, involving nested function calls

我有以下 python 代码 "ex.py" 试图执行两个功能完全相同的并行线程:

import thread

class PrtArg(object):

    def print_name1(tname,*args):
        cnt = 0
        print "Inside print_name1"
        while cnt < 10:
            cnt += 1
            print "%s : %s\n" % (tname, cnt)

    def print_name(tname,*args):
        cnt = 0
        print "Inside print_name"
        while cnt < 10:
            cnt += 1
            print "%s : %s\n" % (tname, cnt)

    def m_prt(self,*args):
        thread.start_new_thread(print_name, (args[0],))
        thread.start_new_thread(print_name1, (args[1],))

我有一个测试套件 "example.robot" 具有以下内容:

*** Settings ***
Documentation    MultiThread Program
Library  ex.PrtArg

*** Test Cases ***
Test title
    [Documentation]  MultiThread
    [Tags]    DEBUG
    m_prt  a  b

*** Keywords

执行此操作时,出现以下错误:

==============================================================================
Ex :: MultiThread Program
==============================================================================
Test title :: MultiThread                                             | FAIL |
NameError: global name 'print_name' is not defined
------------------------------------------------------------------------------
Ex :: MultiThread Program                                            | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================

现在,我遇到了 this,上面写着 "threads should generally communicate with the framework only from the main thread"。上面的代码不就是这么做的吗?我什至没有返回值。只是打印到控制台。正如预期的那样,当我将 "example.robot" 修改为以下内容时,它工作正常:

*** Settings ***
Documentation    MultiThread Program
Library  example4.PrtArg

*** Test Cases ***
Test title
    [Documentation]  MultiThread
    [Tags]    DEBUG
    print_name  a
    print_name1  b

*** Keywords ***

那么,我的问题是:

  1. 如何使用调用 python 库中其他函数的静态关键字获取 运行 测试用例?这可能吗?
  2. 机器人框架支持多线程吗?
  3. 我是不是遗漏了什么?

您应该尝试在静态中声明您的方法,因为它们在 class 的实例中,而 start_new_thread 不接受。

所以你的代码应该是这样的

import thread

class PrtArg(object):
    @staticmethod
    def print_name1(tname,*args):
        cnt = 0
        print "Inside print_name1"
        while cnt < 10:
            cnt += 1
            print "%s : %s\n" % (tname, cnt)

    @staticmethod
    def print_name(tname,*args):
        cnt = 0
        print "Inside print_name"
        while cnt < 10:
            cnt += 1
            print "%s : %s\n" % (tname, cnt)

    def m_prt(self,*args):
        thread.start_new_thread(PrtArg.print_name, (args[0],))
        thread.start_new_thread(PrtArg.print_name1, (args[1],))