0

I have a Discord bot running in python on a Raspberry Pi 3B+. The purpose of this program is to send the lab status to our server by the push of a physical button. I've noticed that the program itself does not always want to cooperate and has been using 100% of a core at all times. I believe the hardware is still healthy and the only other thing that's running is the OS itself. htop reads that the individual program is using 98-101% of a core every time I check.

from os import system
from datetime import date
from time import sleep

from gpiozero import LED, Button
import discord
from discord.ext import commands

import asyncio

# Pull in server token and channel ID
with open('/home/pi/EVC-Discord-Bot/token.txt', 'r') as f:
    TOKEN = f.read()
with open('/home/pi/EVC-Discord-Bot/channel.txt', 'r') as f:
    CHANNEL = f.read()

# Define bot properties
description = '''EVC Lab Bot'''
bot = commands.Bot(command_prefix='!', description=description)

# Define client
client = discord.Client()

# Button and LED GPIO pins
openbutton=Button(17)
pendingbutton=Button(27)
closedbutton=Button(22)
###
openlight=LED(4)
pendinglight=LED(23)
closedlight=LED(24)

# Saved Messages
openmessage=':green_circle: :radio_button: :radio_button: OPEN :green_circle: :radio_button: :radio_button:'
pendingmessage=':radio_button: :yellow_circle: :radio_button: PENDING :radio_button: :yellow_circle: :radio_button:'
closedmessage=':radio_button: :radio_button: :red_circle: CLOSED :radio_button: :radio_button: :red_circle:'

print('Program Initialized')

@bot.event
async def msg(status):
    # Define desired channel
    channel = bot.get_channel(int(CHANNEL))
    # Terminal print channel and status
    print('Bot channel: ' + str(channel))
    print('Channel int: ' + str(CHANNEL))
    print('Sending message...')
    # Print respective status
    if status == 'open':
        await channel.send(openmessage)
    elif status == 'pending':
        await channel.send(pendingmessage)
    elif status == 'closed':
        await channel.send(closedmessage)
    # Terminal print "completed"
    print('Finished.')

@bot.event
async def on_ready():
    # Tell me on_ready() def has started
    print('On ready!')
    # Endless while loop
    while True:
        # Open status
        if openbutton.is_pressed:
            # Show status in terminal
            print('Green Button Pressed')
            # Call server message function
            await msg('open')
            # Toggle open light
            openlight.on()
            # Toggle pending light
            pendinglight.off()
            # Toggle closed light
            closedlight.off()
            # Wait for active button to be released
            openbutton.wait_for_release()
            # Sleep for spam-safety
            sleep(10)
        # Pending status
        elif pendingbutton.is_pressed:
            # Show status in terminal
            print('Yellow Button Pressed')
            # Call server message function
            await msg('pending')
            # Toggle open light
            openlight.off()
            # Toggle pending light
            pendinglight.on()
            # Toggle closed light
            closedlight.off()
            # Wait for active button to be released
            pendingbutton.wait_for_release()
            # Sleep for spam-safety
            sleep(10)
        # Closed status
        elif closedbutton.is_pressed:
            # Show status in terminal
            print('Red Button Pressed')
            # Call server message function
            await msg('closed')
            # Toggle open light
            openlight.off()
            # Toggle pending light
            pendinglight.off()
            # Toggle closed light
            closedlight.on()
            # Wait for active button to be released
            closedbutton.wait_for_release()
            # Sleep for spam-safety
            sleep(10)

# Start bot
bot.run(TOKEN)
# Tell me the bot booted
print('Bot is booted')

1 Answer 1

4

If no button is pressed, your on_ready is basically

async def on_ready():
    print('On ready!')
    while True:
        pass

which of course hogs 100% CPU!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the insight. I'll be considering this on my next revision. Do you know of a way to keep it from looping so CPU hungry like that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.