Richard.sysout 2021-09-10 11:41 采纳率: 100%
浏览 60
已结题

python 线程相关问题:多线程是否会影响子进程里面的print 操作

大概描述一下我的问题:

  • 创建了一个线程,线程内是能够正常运行的unittest框架的测试脚本
  • 测试脚本内加了一个print 操作,线程和线程内测试脚本都正常执行完成,但相关的变量没有print出来
  • 原本猜测可能和unittest有关,但去掉外层的线程创建之后,变量能正常打印,
  • 所以猜测是否和多线程有关,多线程是否会有相关的影响?
    猜测可能是这个原因,还在持续调试中,麻烦帮看看,谢谢
    注:当前只创建了一个线程,之所以使用多线程,是有多个线程同步执行case的需求
  • 部分代码如下:
#执行case 和多线程创建部分的代码
def run_case():
    cur_thread_name = threading.current_thread().name
    report_name = Path.date_time + '_' + cur_thread_name + 'Report.html'
    testlist = unittest.defaultTestLoader.discover(
        start_dir=Path.testcase_path_x_mac
        , pattern='test*.py')
    result = BeautifulReport(testlist)
    result.report(filename=report_name, description='Result of test', log_path=Path.report_path)

class myThread(threading.Thread):

    def __init__(self, thread_id, name):
        threading.Thread.__init__(self)
        self.thread_id = thread_id
        self.name = name

    def run(self):
        print('开启线程:' + self.name + '\n')
        run_case()
        print('退出线程:' + self.name)


if __name__ == '__main__':
    thread_list = []
    for i in range(len(ListData.node_list)):
        thread_name = ListData.node_list[i]
        new_thread = myThread(i, thread_name)
        thread_list.append(new_thread)
    for thread in thread_list:
        thread.start()

    for thread in thread_list:
        thread.join()
  • 部分执行case 部分,加了个print

img

  • 写回答

3条回答 默认 最新

  • Python-ZZY 2021-09-11 12:01
    关注

    会干扰,应该是报错RuntimeError一类的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月26日
  • 已采纳回答 11月18日
  • 创建了问题 9月10日

悬赏问题

  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复