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



我做了一个不和谐的机器人来控制我的仙女灯,并想包括一个聚会功能。我想要聚会功能停止聊天!P停止和开始聊天!p开始。这是我尝试过的:

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小时,没有找到任何适合我的方法。

我不会在多处理中这样做,我只是在一个线程中运行整个事情(这就是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库应该如何工作

注2:我不懂德语,所以可能错过了你对功能的一些预期用途

这个工作的方式是,你让你的派对功能与await asyncio.sleep(random.uniform(***))睡眠,而不是阻塞线程与睡眠。当它处于休眠状态时,它为程序提供了获取新消息并对其作出反应的机会。在运行bot之前,将party函数添加到循环中,并且由于while True循环

,它将永远运行。

像这样:

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

最新更新