通过线程将参数传递给函数

2024-10-02 22:29:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过线程将两个变量传递给函数,如下所示:

from os import system
from sys import exc_info
from threading import Thread
import time


def play(file, priority):

    try:
        if priority == 0:
            system('ps aux | grep mpg321 | grep -v "grep mpg321" | awk \'{print $2}\' | xargs kill -9')

        statement = 'sudo -u pi mpg321 -g 1 -q -a bluetooth sound/' + file
        system(statement)

    except:
        print('There was an error playing the sound - ' + str(exc_info()[0]))

    finally:
        pass

t1Sound = 'presence.mp3'
t2Sound = 'ultra.mp3'

t1 = Thread(target=play(), args=(t1Sound, 0))
t2 = Thread(target=play(), args=(t2Sound, 1))

t1.start()
time.sleep(2)
t2.start()

但不知怎么的,我总是得到下面的错误:

sudo python test.py
Traceback (most recent call last):
  File "test.py", line 25, in <module>
    t1 = Thread(target=play(), args=(t1Sound, 0))
TypeError: play() takes exactly 2 arguments (0 given)

你们知道怎么修吗?我应该如何正确地传递这些变量


Tags: fromimportinfotargetplaytimeargssystem