在pygame中进行线程处理

时间:2014-12-19 18:11:56

标签: python multithreading pygame subprocess

我在Linux上用python和pygame编写了一个程序。我的问题是,当我调用我的函数在调制解调器上拨出然后发送消息时,程序会暂停,直到函数完成。我需要一种方法subprocess或线程函数。我是python和pygame的新手,所以这可能很简单,但我尝试过的一切都失败了。

另外,我确信可能有一种更优雅的方式来处理声音和暂停。第一个sleep给出了调制解调器的时间。接下来的两个是在单词和句子之间自然发出声音暂停,最后一个是让整个脚本有时间在调制解调器挂起之前发送消息。

我正在调用函数并传递这样的变量:

A = 'Electronic_Chime.mp3'
B = 'please_check.mp3'
C = 'three.mp3'

contact_user(A,B,C)

这就是功能:

def contact_user( A, B, C ):
  ser.write("ATDT441\r")  # Attention - Dial via tone number
  time.sleep(6)

  pygame.mixer.music.load(A)
  pygame.mixer.music.play()
  time.sleep(2)

  pygame.mixer.music.load(B)
  pygame.mixer.music.play(1)
  time.sleep(2)

  pygame.mixer.music.load(C)
  pygame.mixer.music.queue(C)
  pygame.mixer.music.play()

  time.sleep(10)

任何想法或建议都将受到赞赏。

PS。我试过了:

thread1 = threading.Thread(contact_user(A,B,C))
thread1.start()

即使我有线程contact_user(A,B,C),该程序似乎表现完全相同。

1 个答案:

答案 0 :(得分:1)

我的主要问题是我没有正确地将我的论点传递给我的帖子。以下是适合我的代码。

我的功能:

def contact_user(A,B,C):
  ser.write("ATDT411\r")  # Attention - Dial Tone 411
  time.sleep(5)  # Wait for modem to finish dialing
  pygame.mixer.music.load(A)
  pygame.mixer.music.play()  # Play attention chime
  time.sleep(2)  # Wait long enough for first message chime to play before moving on
  pygame.mixer.music.load(B)
  pygame.mixer.music.play(1) # Play next message
  time.sleep(2)  # Wait long enough for second message to play before moving on
  pygame.mixer.music.load(C) # Load last message
  pygame.mixer.music.queue(C)  # Queue second instance of last message
  pygame.mixer.music.play()  # Play last message twice
  time.sleep(10)  # Wait long enough for last message to play
  ser.write("ATH\r")  # Attention - Hang up line
  ser.write("ATZ\r")  # Attention - Reset modem
  ser.close()

从主程序调用函数:

t = threading.Thread(name = 'dial', target = contact_user, args = (A,B,C))
t.start()

感谢所有帮助我的人。非常感谢。

相关问题