0

Sorry guys for the very newbish question.

I just created a small program using PySimpleGUI and BeautifulSoup which reads live stocks from the TMXMoney website. I know it's quite bad but I am very new to Python (this is my first program actually).

Unfortunately, my program is running a bit slow. When I CMD + Tab to a browser and back to my app, it takes a few seconds for that to go through.

I know I probably have some piece of code that is bottlenecking the whole program, so I was wondering if anybody could help me out in identifying what that bottleneck is?

Here is the code:

import PySimpleGUI as sg
import time
import bs4
import requests

def parsePrice(sym):
    r = requests.get("https://web.tmxmoney.com/quote.php?qm_symbol=" + sym)     #Canadian stocks source (TSX)
    soup = bs4.BeautifulSoup(r.text,"html.parser")
    try:
        price = soup.find("div", {"class":"labs-symbol"}).find("span", {"class":"price"}).find("span").text
        return price
    except:
        return "Cannot find"

sg.theme('DarkBrown1')

layout = [[sg.Input(key="i1",font=('Helvetica', 20),size = (10,0)), sg.Text("0.00",size=(6,0), font=('Helvetica', 20), key="o1")],
       [sg.Input(key="i2",font=('Helvetica', 20),size = (10,0)), sg.Text("0.00",size=(6,0),font=('Helvetica', 20), key="o2")],
       [sg.Input(key="i3",font=('Helvetica', 20),size = (10,0)), sg.Text("0.00",size=(6,0),font=('Helvetica', 20), key="o3")],
       [sg.Input(key="i4",font=('Helvetica', 20),size = (10,0)), sg.Text("0.00",size=(6,0),font=('Helvetica', 20), key="o4")],
       [sg.Input(key="i5",font=('Helvetica', 20),size = (10,0)), sg.Text("0.00",size=(6,0),font=('Helvetica', 20), key="o5")],
       [sg.Input(key="i6",font=('Helvetica', 20),size = (10,0)), sg.Text("0.00",size=(6,0),font=('Helvetica', 20), key="o6")]]

window = sg.Window("TSX Stocks", layout)

while True:                              # Event Loop
    event, values = window.read(timeout=2000)
    if event in (None, 'Quit'):          # if user closed the window using X or clicked Quit button
        break

    if values['i1'] is not "":
        window['o1'].update(parsePrice(values['i1']))
    if values['i2'] is not "":
        window['o2'].update(parsePrice(values['i2']))
    if values['i3'] is not "":
        window['o3'].update(parsePrice(values['i3']))
    if values['i4'] is not "":
        window['o4'].update(parsePrice(values['i4']))
    if values['i5'] is not "":
        window['o5'].update(parsePrice(values['i5']))
    if values['i6'] is not "":
        window['o6'].update(parsePrice(values['i6']))

    time.sleep(1)

window.close()
Georgi
  • 29
  • 4

1 Answers1

1

The cause is probably that it's slowing down because it's waiting for all the input. The submit button is added and the data is retrieved by pressing it and returned to the event loop. github demo programThere are plenty of examples here for your reference.

import PySimpleGUI as sg
import time
import bs4
import requests

def parsePrice(sym):
    r = requests.get("https://web.tmxmoney.com/quote.php?qm_symbol=" + sym)     #Canadian stocks source (TSX)
    soup = bs4.BeautifulSoup(r.text,"html.parser")
    try:
        price = soup.find("div", {"class":"labs-symbol"}).find("span", {"class":"price"}).find("span").text
        return price
    except:
        return "Cannot find"

sg.theme('DarkBrown1')

layout = [[sg.Input(key="i1",font=('Helvetica', 20),size = (10,0)), sg.Submit(key='-sym1-'), sg.Text("0.00",size=(6,0), font=('Helvetica', 20), key="o1")],
       [sg.Input(key="i2",font=('Helvetica', 20),size = (10,0)), sg.Submit(key='-sym2-'), sg.Text("0.00",size=(6,0),font=('Helvetica', 20), key="o2")],
       [sg.Input(key="i3",font=('Helvetica', 20),size = (10,0)), sg.Submit(key='-sym3-'), sg.Text("0.00",size=(6,0),font=('Helvetica', 20), key="o3")],
       [sg.Input(key="i4",font=('Helvetica', 20),size = (10,0)), sg.Submit(key='-sym4-'), sg.Text("0.00",size=(6,0),font=('Helvetica', 20), key="o4")],
       [sg.Input(key="i5",font=('Helvetica', 20),size = (10,0)), sg.Submit(key='-sym5-'), sg.Text("0.00",size=(6,0),font=('Helvetica', 20), key="o5")],
       [sg.Input(key="i6",font=('Helvetica', 20),size = (10,0)), sg.Submit(key='-sym6-'), sg.Text("0.00",size=(6,0),font=('Helvetica', 20), key="o6")],
        [sg.Quit()]]

window = sg.Window("TSX Stocks", layout)

while True:                              # Event Loop
    event, values = window.read()
    print(event, values)
    if event in (None, 'Quit'):          # if user closed the window using X or clicked Quit button
        break

    if event == '-sym1-':
        symbol = values['i1']
        price = parsePrice(symbol)
        window['o1'].update(price)
    if event == '-sym2-':
        symbol = values['i2']
        price = parsePrice(symbol)
        window['o2'].update(price)
    if event == '-sym3-':
        symbol = values['i3']
        price = parsePrice(symbol)
        window['o3'].update(price)
    if event == '-sym4-':
        symbol = values['i4']
        price = parsePrice(symbol)
        window['o4'].update(price)
    if event == '-sym5-':
        symbol = values['i5']
        price = parsePrice(symbol)
        window['o5'].update(price)
    if event == '-sym6-':
        symbol = values['i6']
        price = parsePrice(symbol)
        window['o6'].update(price)    

#     time.sleep(1)

window.close()
r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • Upon reviewing your code today, I was very disappointed that the way you modified my app, the stocks I am getting now no longer update in real time. Rather, I have to manually click "Submit" to get the newest stock quote. – Georgi May 19 '20 at 15:26
  • Simply put, I think it's the heaviness of the real-time processing and the lightness of the processing each time.Simply put, I think it's the heaviness of the real-time processing and the lightness of the processing each time. – r-beginners May 20 '20 at 01:40