python - 缺少必需的参数,类中的多处理
问题描述
所以最近我想尝试在我的工作脚本中使用多处理,所以我做了这个假人,只是为了看看我是否可以让它与一个类一起工作,我得到了错误。
谁能告诉我如何解决这个问题?
from multiprocessing import Process
class A:
def __init__(self):
print("Starting")
def login(self, auth_user, auth_pass):
time.sleep(1)
print(f'Username: {auth_user}\nPassword: {auth_pass}')
def say_hello(self, how_many_times):
for i in range(how_many_times):
time.sleep(1)
print(f'Times: {i}')
if __name__ == '__main__':
a = A()
auth = {
'username': 'jnk',
'password': 'test'
}
p1 = Process(target=a.login(), args=(auth['username'], auth['password'],))
p2 = Process(target=a.say_hello(), args=(5,))
p1.start()
p2.start()
p1.join()
p2.join()
错误:
Starting
Traceback (most recent call last):
File "test.py", line 28, in <module>
p1 = Process(target=a.login(), args=(auth['username'], auth['password'],))
TypeError: login() missing 2 required positional arguments: 'auth_user' and 'auth_pass'
感谢您提前回复。
解决方案
您的问题与此问题非常相似,您会在那里找到详细的答案。简而言之,更换
def login(self, auth_user, auth_pass):
和
def login (event):
该错误消息消失了,但我收到另一条错误消息,指出time
未定义。但我怀疑它是在其他地方定义的,在不在这里的部分代码中。
推荐阅读
- javascript - 初始化 RTCPeerConnection 时如何禁用 Chromium 入站权限请求
- java - lwjgl 中的缓冲实用程序导致内存泄漏
- r - 在R中使用map()函数循环时如何获取变量的名称而不是x?
- c# - 如何将 gsm 调制解调器自动连接到可用的 COM 端口(没有组合框)
- matlab - 在 Matlab 中应用拉普拉斯滤波器
- javascript - 新年计时器无法设置属性
- javascript - 无法向 Pastebin API 发出请求
- command - TypeError:无法读取 null 的属性“角色”
- reactjs - 使用 firebase 数据库和反应钩子设置和更新表单输入
- php - PhP Group 数组结果或转储数组结果