在python中线程化多个函数

时间:2017-02-23 01:12:44

标签: python multithreading twitter pygame

我创建了一个程序,通过Twitter流式传输,并根据推文生成的结果,使用pygame库播放音乐。下面是我的代码示例。

class listener(StreamListener):

def on_status(self, status):
    global mood_happy, mood_sad, mood_angry, mood_shocked, mood_romantic

    try:
        # print status
        tweet_text = status.text
        for mood_n_score in [[happy, 'mood_happy'], [sad, 'mood_sad'], [angry, 'mood_angry'],
                             [shocked, 'mood_shocked'], [romantic, 'mood_romantic']]:
            lst_mood = mood_n_score[0]
            type_mood = mood_n_score[1]

            for mood in lst_mood:
                if mood in tweet_text:
                    if type_mood == 'mood_happy':
                        mood_happy += 1
                    elif type_mood == 'mood_sad':
                        mood_sad += 1
                    elif type_mood == 'mood_angry':
                        mood_angry += 1
                    elif type_mood == 'mood_shocked':
                        mood_shocked += 1
                    else:
                        mood_romantic += 1
                    break

        print('\n----------------')
        print 'mood_happy:', mood_happy
        print 'mood_sad:', mood_sad
        print 'mood_angry:', mood_angry
        print 'mood_shocked:', mood_shocked
        print 'mood_romantic:', mood_romantic



        top_mood=max(mood_happy,mood_sad,mood_angry,mood_shocked,mood_romantic)
        if top_mood==mood_happy:
            print "the mood is: happy"
            pygame.mixer.music.load(file.mp3)
            pygame.mixer.music.play()

正如你所看到的,我有一个流媒体课程,它不断地通过推特流动,打印出顶级情绪。当我运行我的代码来播放mp3文件时,流式传输停止,只播放音乐。如何通过推特制作节目流并同时播放音乐?

谢谢!

1 个答案:

答案 0 :(得分:0)

我从未使用过pygame,但基于它的作用,我认为我可以认为它不是线程安全的。

我要做的是使用threading模块在​​线程中使用流媒体代码,让音乐播放逻辑在主线程上不断等待threading.Event设置。

import threading
import pygame


new_mood_event = threading.Event()


class TwitterStreamer(StreamListener):
    def run(self):
        while True:  # keep the streamer going forever
            pass  # define your code here

    def on_status(self, status):
        # ... Define your code here
        if top_mood == mood_happy:
            new_mood_event.mp3_file_path = 'happy_file.mp3'
            new_mood_event.set()  # alert the main thread we have a new mood to play


if __name__ == '__main__':
    twitter_streamer = TwitterStreamer()
    streaming_thread = threading.Thread(target=twitter_streamer.run)  # creates a thread that will call `twitter_streamer.run()` when executed
    streaming_thread.start()  # starts the thread

    # everything from here will be run in the main thread
    while True:  # creates an "event loop"
        new_mood_event.wait()  # blocks the main thread until `new_mood_event.set()` is called by `on_status`
        new_mood_event.clear()  # clears the event. if we don't clear the event, then `new_mood_event.wait()` will only block once
        pygame.mixer.music.load(new_mood_event.mp3_file_path)
        pygame.mixer.music.play()