0

i have less than 3 months of python programming under my belt but basically, i have a program that pulls values from the CoinGecko API indefinitely and creates processes so the functions that pull the data can run independently from one another, but id like for it to break its loop after i press the specified hotkey which is set to 'q'. whenever i press the hotkey, nothing happens and the loop just keeps running. i've tried using the keyboard.read_key() function, but that just stops my program from running until i press the q button, which causes the program to run the loop once and then close. i have no idea why the is_pressed() function refuses to work and id like some help from more advanced people

Piece of Code in question:

from multiprocessing.dummy import freeze_support                
from pycoingecko import CoinGeckoAPI
import time
from multiprocessing import Process
from multiprocessing import Pool
import multiprocessing
import keyboard as kb
import psutil




cg = CoinGeckoAPI()


class CGCoin:
    def __init__(self, coinname, coinid):
        self.coinname = coinname
        self.coinid = coinid
   

    def pulldata(self):
       
        while True:
             
            wishtoquit = False
            if kb.is_pressed('Q'):
                print('ending after this loop')
                wishtoquit = True
                
            
            timestarted = time.asctime()
            
            self.prices = []
            self.daychanges = []
            self.volumes = []
            self.marketcaps = []
            self.weekchanges = []
            self.highs = []
            self.lows = []
            self.times = []
            print(f'starting {self.coinname} reading at {timestarted}')
            loops = 0 
            maxloops = 2
            while loops < maxloops:
                
                time.sleep(15)
                coin = cg.get_coin_by_id(f'{self.coinid}')
                time.sleep(5)
                coinvalues = coin.get('market_data')
                coinprices = coinvalues.get('current_price')
                coinvolumes = coinvalues.get('total_volume')
                mrktcaps = coinvalues.get('market_cap')
                dayhigh = coinvalues.get('high_24h')
                daylow = coinvalues.get('low_24h')
                daychangepercentage = coinvalues.get('price_change_percentage_24h')
                weekchangepercentage = coinvalues.get('price_change_percentage_7d')
                coinprice = coinprices.get('usd') 
                coinvolume = coinvolumes.get('usd')
                coincap = mrktcaps.get('usd')
                coindayhigh = dayhigh.get('usd')
                coindaylow = daylow.get('usd')
                timepulled = time.asctime()


                self.prices.append(coinprice)
                self.daychanges.append(daychangepercentage)
                self.volumes.append(coinvolume)
                self.marketcaps.append(coincap)
                self.weekchanges.append(weekchangepercentage)
                self.highs.append(coindayhigh)
                self.lows.append(coindaylow)
                self.times.append(timepulled)
                loops = loops + 1 
                print(loops)
            timeended = time.asctime()



            })
            print(f'stopping {self.coinname} reading at {timeended}')
            if wishtoquit:
                print('ending loops')
                
                break

            time.sleep(5)

           


            
bitcoin = CGCoin('Bitcoin', 'bitcoin')
ethereum = CGCoin('Ethereum', 'ethereum')
if __name__ == '__main__':
    freeze_support()
    btcpul = Process(target=bitcoin.pulldata, name=bitcoin.coinname)
    btcpul.start()
 

if anyone has any ideas or fully-functional workarounds id really like to hear them. id be extremely grateful for any help recieved

Gabe Itch
  • 3
  • 4

1 Answers1

0

It looks like PyPi keyboard needs root permissions on linux.

You could just do kb.on_press_key("p", lambda _: sys.exit(0)) and just do a sys.exit(0) to end the script.

If you're running this in the terminal you should just be able to press ctrl+c to interrupt its execution.

ref: How to detect key presses?

Ragnoroct
  • 105
  • 2
  • 5
  • the ctrl c works but whenever i press p, nothing happens. it cant be that it needs root permissions because the script is running on windows. im starting to think its a problem with my keyboard itself or something else. ill settle with the ctrl c and probably come back to this later or use a different module – Gabe Itch Mar 09 '22 at 20:12