Python-如何在构建线程步骤中不启动线程
我有以下代码:Python-如何在构建线程步骤中不启动线程,python,multithreading,Python,Multithreading,我有以下代码: import threading tokens = [here are list with tokens] def geter(token): print('FROM METHOD') for token in tokens: t = threading.Thread(target=geter(token), args=(token,)) print('THERE METHOD "GETER" SHOULD NOT RUN') t.start
import threading
tokens = [here are list with tokens]
def geter(token):
print('FROM METHOD')
for token in tokens:
t = threading.Thread(target=geter(token), args=(token,))
print('THERE METHOD "GETER" SHOULD NOT RUN')
t.start()
print('THERE METHOD "GETER" WAS EXECUTED')
FROM METHOD
THERE METHOD "GETER" SHOULD NOT RUN
THERE METHOD "GETER" WAS EXECUTED
THERE METHOD "GETER" SHOULD NOT RUN
FROM METHOD
THERE METHOD "GETER" WAS EXECUTED
我只需要在“t.start”之后启动方法“geter”,而不是在构建线程步骤上
但在我的例子中,我看到在方法'geter'运行后有2个打印。
我有这张照片:
import threading
tokens = [here are list with tokens]
def geter(token):
print('FROM METHOD')
for token in tokens:
t = threading.Thread(target=geter(token), args=(token,))
print('THERE METHOD "GETER" SHOULD NOT RUN')
t.start()
print('THERE METHOD "GETER" WAS EXECUTED')
FROM METHOD
THERE METHOD "GETER" SHOULD NOT RUN
THERE METHOD "GETER" WAS EXECUTED
THERE METHOD "GETER" SHOULD NOT RUN
FROM METHOD
THERE METHOD "GETER" WAS EXECUTED
如何仅在第一次和第二次打印之间运行方法“geter”(在步骤:t.start()上)?
我需要这张照片:
import threading
tokens = [here are list with tokens]
def geter(token):
print('FROM METHOD')
for token in tokens:
t = threading.Thread(target=geter(token), args=(token,))
print('THERE METHOD "GETER" SHOULD NOT RUN')
t.start()
print('THERE METHOD "GETER" WAS EXECUTED')
FROM METHOD
THERE METHOD "GETER" SHOULD NOT RUN
THERE METHOD "GETER" WAS EXECUTED
THERE METHOD "GETER" SHOULD NOT RUN
FROM METHOD
THERE METHOD "GETER" WAS EXECUTED
谢谢问题是您正在调用(调用)函数,而不是将其传递给
线程
类,因此
t = threading.Thread(target=geter(token), args=(token,))
这样做:
t = threading.Thread(target=geter, args=(token,))
请注意,您不需要提供两次令牌
,这应该会向您暗示出问题
target
参数应该是一个函数(如geter
)或None
,但是由于您正在调用函数,该函数返回None
(因为它不返回任何内容),因此不会出现错误。但是,您确实会遇到这种意外行为