我如何在 python 中使用多线程停止正在运行的程序

时间:2021-01-26 10:40:09

标签: python multithreading multiprocessing discord.py

我制作了一个 Discord-Bot 来控制我的小灯,并希望包含派对功能。 我希望聚会功能在聊天 !p stop 时停止,并在聊天 !p start 时启动。这是我试过的:

import multiprocessing

import time
import RGB as rgb
import discord
import random

dc = discord.Client()

rot = 1
grün = 2
blau = 3

#Following is because of RGB toggeling
global statr
global statg
global statb
statr = "aus"
statg = "aus"
statb = "aus"
#Ok toggeling stopped

global statP
statP = 1

def party():
    rgb.start()
    while statP != 0:
        anAus = random.randint(1, 2)
        faabe = random.randint(1, 4)
        if anAus == 1:
            rgb.on(faabe)
        if anAus == 2:
            rgb.off(faabe)
#        dur = dur - 1
        time.sleep(random.uniform(0.02, 0.07))

global proc
proc = multiprocessing.Process(target=party, args=())

@dc.event
async def on_message(message):
    if message.author.id == dc.user.id:
        return
    if message.content.startswith("!p start"):
        #msg = message.content
        #rest = msg.replace("!party ", "")
        #msg = "Mache " + rest + " Durchläufe lang Party."
        #await message.channel.send(msg)
#        Thread(target = party).start()
        statP = 1
        proc.start()
    if message.content.startswith("!p stop"):
        statP = 0

我该怎么做?我从这里尝试了其他一些方法,并在谷歌上搜索了大约 2 小时,但找不到任何对我有用的东西。

2 个答案:

答案 0 :(得分:2)

像这样:

import threading

import time
import RGB as rgb
import discord
import random

dc = discord.Client()

rot = 1
grün = 2
blau = 3

global statr
global statg
global statb
statr = "aus"
statg = "aus"
statb = "aus"

global statP
statP = 1

def party():
    rgb.start()
    while statP != 0:
        anAus = random.randint(1, 2)
        faabe = random.randint(1, 4)
        if anAus == 1:
            rgb.on(faabe)
        if anAus == 2:
            rgb.off(faabe)
#        dur = dur - 1
        time.sleep(random.uniform(0.02, 0.07))

#global proc
#proc = multiprocessing.Process(target=party, args=())

@dc.event
async def on_message(message):
    if message.author.id == dc.user.id:
        return
    if message.content.startswith("!p start"):
        #msg = message.content
        #rest = msg.replace("!party ", "")
        #msg = "Mache " + rest + " Durchläufe lang Party."
        #await message.channel.send(msg)
        statP = 1
        threading.Thread(target = party).start()
#        proc.start()
    if message.content.startswith("!p stop"):
        statP = 0

答案 1 :(得分:2)

我不会用多处理来做这件事,我只是在一个线程中运行整个事情(这就是 asyncio 的用途),如下所示:

import time
import RGB as rgb
import discord
import random
import asyncio

dc = discord.Client()

rot = 1
grün = 2
blau = 3

statr = "aus"
statg = "aus"
statb = "aus"

statP = 1

async def party():
    rgb.start()
    while True:
        if statP != 0:
            anAus = random.randint(1, 2)
            faabe = random.randint(1, 4)
            if anAus == 1:
                rgb.on(faabe)
            if anAus == 2:
                rgb.off(faabe)
            #        dur = dur - 1
        await asyncio.sleep(random.uniform(0.02, 0.07))


@dc.event
async def on_message(message):
    global statP
    if message.author.id == dc.user.id:
        return
    if message.content.startswith("!p start"):
        message.channel.send('started the party')
        statP = 1
    if message.content.startswith("!p stop"):
        statP = 0

dc.loop.create_task(party())

dc.run(TOKEN)

note1:我不知道你的 RGB 库应该如何工作

note2:我不会德语,所以可能错过了您对功能的一些预期用途

这种工作方式是让您的派对功能使用 await asyncio.sleep(random.uniform(***)) 休眠,而不是使用 sleep 阻塞线程。当它处于休眠状态时,它为您的程序提供了获取新消息并对其做出反应的机会。在运行机器人之前,party 函数被添加到循环中,它会因为 while True 循环而永远运行