在 Python 中线程化时出现 AssertionError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15349997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:53:00  来源:igfitidea点击:

AssertionError when threading in Python

pythonmultithreading

提问by djcmm476

I'm trying to run some simple threading in Python using:

我正在尝试使用以下方法在 Python 中运行一些简单的线程:

t1 = threading.Thread(analysis("samplequery"))
t1.start()

other code runs in here

t1.join()

Unforunately I'm getting the error:

不幸的是,我收到错误消息:

"AssertionError: group argument must be none for now"

“断言错误:组参数现在必须为无”

I've never implemented threading in Python before, so I'm a bit unsure as to what's going wrong. Does anyone have any idea what the problem is?

我以前从未在 Python 中实现过线程,所以我有点不确定出了什么问题。有谁知道问题是什么?

I'm not sure if it's relevant at all, but analysis is a method imported from another file.

我不确定它是否相关,但分析是一种从另一个文件导入的方法。

I had one follow up query as well. Analysis returns a dictionary, how would I go about assigning that for use in the original method?

我也有一个后续查询。分析返回一个字典,我将如何分配它以用于原始方法?

Thanks

谢谢

采纳答案by Martijn Pieters

You want to specify the targetkeyword parameter instead:

您想改为指定target关键字参数:

t1 = threading.Thread(target=analysis("samplequery"))

You probably meant to make analysisthe run target, but 'samplequerythe argument when started:

您可能打算制作analysis运行目标,但启动时'samplequery的参数:

t1 = threading.Thread(target=analysis, args=("samplequery",))

The first parameter to Thread()is the groupargument, and it currently only accepts Noneas the argument.

to 的第一个参数Thread()group参数,它目前只接受None作为参数。

From the threading.Thread()documentation:

threading.Thread()文档

This constructor should always be called with keyword arguments. Arguments are:

  • groupshould be None; reserved for future extension when a ThreadGroupclass is implemented.
  • targetis the callable object to be invoked by the run()method. Defaults to None, meaning nothing is called.

应始终使用关键字参数调用此构造函数。参数是:

  • 应该是None;为将来ThreadGroup实现类时的扩展保留。
  • targetrun()方法要调用的可调用对象。默认为None,意味着什么都不调用。

回答by g.d.d.c

You need to provide the targetattribute:

您需要提供target属性:

t1 = threading.Thread(target = analysis, args = ('samplequery',))