3n20
3n20

Reputation: 41

How can i stop a running procees with multithreading in python

I made a Discord-Bot to control my fairy lights and wanted to include a party function. I want the party function to stop when chatting !p stop and to start when chatting !p start. This is what I tried:

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

How can i make that? I tried some other ways from here and googled about 2h long and couldnt find anything that worked for me.

Upvotes: 1

Views: 71

Answers (2)

FierySpectre
FierySpectre

Reputation: 661

I wouldn't do this with multiprocessing, I'd just run the whole thing in one thread (thats what asyncio is made for) like this:

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: I have no clue how your RGB library is supposed to work

note2: I don't know german so might have missed some of your intended use of functionality

The way this works is that you let your party function slumber with await asyncio.sleep(random.uniform(***)) instead of blocking the thread with sleep. While it slumbers it gives the opportunity to your program to get new messages and react to them. Before running the bot the partyfunction is added to the loop, and it'll run for ever because of the while True loop

Upvotes: 2

3n20
3n20

Reputation: 41

Like this:

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

Upvotes: 2

Related Questions