首页 > 解决方案 > 缺少必需的参数,类中的多处理

问题描述

所以最近我想尝试在我的工作脚本中使用多处理,所以我做了这个假人,只是为了看看我是否可以让它与一个类一起工作,我得到了错误。

谁能告诉我如何解决这个问题?

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'

感谢您提前回复。

标签: pythonmultiprocessing

解决方案


您的问题与此问题非常相似,您会在那里找到详细的答案。简而言之,更换

def login(self, auth_user, auth_pass):

def login (event):

该错误消息消失了,但我收到另一条错误消息,指出time未定义。但我怀疑它是在其他地方定义的,在不在这里的部分代码中。


推荐阅读