多线程函数正好接受6个参数(给定0个)错误

我已经创建了简单的线程函数,但是即使我已经全部传递了6个参数,但我还是得到了传递0个参数的错误。我已经尝试过同时使用args和kwargs,但仍然有相同的错误

下面是我的代码

import time
import datetime
import threading

def get_time():
    datestart = datetime.datetime.now() - datetime.timedelta(minutes = 60)
    dateend = datetime.datetime.now()
    timeprevious = int(time.mktime(datestart.timetuple()) * 1000)
    timenow = int(time.mktime(dateend.timetuple()) * 1000)
    return timeprevious,timenow

def customer(src_ip,dst_ip,host_ip,index_name,timeprevious,timenow):
  print(src_ip)
  print(dst_ip)
  print(host_ip)
  print(index_name)
  print(timeprevious)
  print(timenow)

host_name = ['host_name1','host_name2','host_name3']
host_ip = ['host_ip1','host_ip2','host_ip3']
index_name = ['index_name1',' index_name2','index_name3']
src_ip = ['Src_IP','source_ip','SourceAddress']
dst_ip = ['Dst_IP','destination_ip','DestinationAddress']

timeprevious,timenow = get_time()

threads = []                                                                
for i in range(len(host_name)):
  try:
    # t = threading.Thread(target=customer(),args=(src_ip[i],dst_ip[i],host_ip[i],index_name[i],timenow))
    t = threading.Thread(target=customer(),kwargs={'src_ip': src_ip[i],'dst_ip':dst_ip[i],'host_ip': host_ip[i],'index_name': index_name[i],'timeprevious': timeprevious,'timenow': timenow })

    threads.append(t)
    t.start()
  except Exception as e:
    print('error ' + host_name[i])
    print(e)

for t in threads:                                                           
  t.join()

这是我遇到的错误 customer() takes exactly 6 arguments (0 given)。您可以在评论中看到,我仍然使用kwargs来解决错误,但我仍然没有运气。这也是传递多个参数的正确方法吗?

ppwpp 回答:多线程函数正好接受6个参数(给定0个)错误

您在以下一行上遇到了问题:

t = threading.Thread(target=customer(),kwargs={'src_ip': src_ip[i],'dst_ip':dst_ip[i],'host_ip': host_ip[i],'index_name': index_name[i],'timeprevious': timeprevious,'timenow': timenow })*emphasized text*

target=customer()将目标函数的值设置为调用customer()的返回值。要传递对客户功能的引用,请使用target=customer

本文链接:https://www.f2er.com/3114504.html

大家都在问