Python 缺少必需的参数,类中有多个处理

Python 缺少必需的参数,类中有多个处理,python,multiprocessing,Python,Multiprocessing,所以最近我想尝试在我的工作脚本中使用多处理,所以我制作了这个假人,只是想看看我是否可以使它与类一起工作,并且我得到了错误 有人能告诉我如何解决这个问题吗 from multiprocessing import Process class A: def __init__(self): print("Starting") def login(self, auth_user, auth_pass): time.sleep(1) p

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

有人能告诉我如何解决这个问题吗

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'
启动
回溯(最近一次呼叫最后一次):
文件“test.py”,第28行,在
p1=进程(target=a.login(),args=(auth['username'],auth['password']))
TypeError:login()缺少2个必需的位置参数:“auth\u user”和“auth\u pass”

感谢您提前回复。

您的问题与非常相似,您可以在那里找到详细的答案。换

def登录(self、auth\u user、auth\u pass):

def登录(事件):


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

您的问题与非常相似,您可以在那里找到详细的答案。换

def登录(self、auth\u user、auth\u pass):

def登录(事件):

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

您正在调用函数-您必须将它们提供给流程:

import time

if __name__ == '__main__':

    a = A() 
    auth = { 'username': 'jnk', 'password': 'test' }

    # provide the functions - DO NOT CALL THEM - do not place () after them:    
    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
Username: jnk
Password: test
Times: 0
Times: 1
Times: 2
Times: 3
Times: 4
您正在调用函数-您必须向流程提供它们:

import time

if __name__ == '__main__':

    a = A() 
    auth = { 'username': 'jnk', 'password': 'test' }

    # provide the functions - DO NOT CALL THEM - do not place () after them:    
    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
Username: jnk
Password: test
Times: 0
Times: 1
Times: 2
Times: 3
Times: 4

不是原因-原因是他调用的函数只应提供由进程调用的函数。实际上,您将使用2个参数和self的
def login
类实例函数减少为0个参数的登录函数,其中通常命名为
self
的实例由name
event
调用时,它会在另一个线程中悄无声息地崩溃,并且不会打印给定的参数。。。从本质上讲,您将中断的代码转换为静默中断的代码。这不是原因-原因是他调用的函数只应提供由进程调用的函数。您从本质上讲,将使用2个参数和self的
def login
类实例函数减少为0个参数的登录函数,而实例通常在该函数中命名
self
由名称
event
本身提供。调用时,它会在另一个线程中悄无声息地崩溃,并且不会打印给定的参数。。。实际上,您将中断的代码转换为静默中断的代码。