繁体   English   中英

在python中线程化多个函数

[英]Threading multiple functions in python

我创建了一个程序,该程序通过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()

如您所见,我有一个流媒体类,它连续地通过Twitter流媒体并显示最佳心情。 当我运行代码播放mp3文件时,流媒体停止播放,仅播放音乐。 如何使我的程序通过Twitter流播并同时播放音乐?

谢谢!

我从未使用过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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM