2

I am checking the internet connectivity on GUI button press and reading the status value to process other internet dependent Tasks.

Issue : The screen become unresponsive until the instatus thread is finished.

I want the GUI to read the Internet status parallel to Screen function and store the status in a variable but I believe the join method blocks the code until I receive the output. If anyone can help me out with this problem, It would be greatly appreciated.Thanks in advance.

Internet connection checking python script :

import socket

def is_connected(hostname):
  try:
    # connect to the host -- tells us if the host is actually
    # reachable
    s = socket.create_connection((hostname, 80), 2)
    if s is not None:
        s.close()
    return True
  except:
     pass
  return False

Main Program code snippet :

def CheckRoutine(root):
    global PageTimer
    global mainScreen
    global SecScreenClick
    global optionSelect
    optionSelect = 0
    if SecScreenClick == False:
        SecScreenClick = True
        mainScreen = root
        if initScroller is not None:
                root.after_cancel(initScroller)
        root.withdraw()
        timeCount = 0
        optionSelect = 0
        endRecycleCount = False
        master1 = tk.Toplevel()
        master1.attributes('-fullscreen', True)
        lblBackground = Label(master1, width=ws, height=hs, image=BGEngS2)
        lblBackground.photo = BGEngS2
        lblBackground.pack(fill=BOTH, expand=YES)
        operatorButton = tk.Button(lblBackground, highlightthickness=0, bd=0, borderwidth=0, image=OpLoginS2, command=lambda : winRecycling(master1, 4, backThr))
        operatorButton.image = OpLoginS2
        operatorButton.place(x = 20, y = 670)
        engButton = tk.Button(lblBackground, highlightthickness=0, bd=0, borderwidth=0, image=langEngS2, command=lambda : winRecycling(master1, 1, backThr))
        engButton.image = langEngS2
        engButton.place(x=950, y=20)
        hindiButton = tk.Button(lblBackground, highlightthickness=0, bd=0, borderwidth=0, image=langHindiS2, command=lambda : winRecycling(master1, 2, backThr))
        hindiButton.image = langHindiS2
        hindiButton.place(x=950, y=397)
        PageTimer = Label(lblBackground, width=10, height=2, font=('Times', 20, 'bold'), bg='white')
        PageTimer.place(x=95, y=15)
        settings.timerStatus = 1
        cdTimer.screenTimer(PageTimer, 0, 20)
        backThr = master1.after(20000, lambda: backRoutine(master1, mainScreen))
        master1.after(300, lambda : InternetStatus_Check())
        master1.mainloop()
        return

def InternetStatus_Check():
        global internetCheck_flag
        inStatus = threading.Thread(target=lambda q, arg1: q.put(internetCheck.is_connected(arg1)),args=(que, '1.1.1.1'))
        inStatus.start()
        inStatus.join()
        internetCheck_flag = que.get()
        print internetCheck_flag
   
Button1= tk.Button(MainScreen, image=start_image, highlightthickness=0, borderwidth=2, relief="raised", command=lambda : CheckRoutine(root))
Button1.image = start_image
Button1.place(x=980, y=45)
Krushi
  • 21
  • 4
  • I cannot spot anything that would cause the window to be unresponsive but `cdTimer.screenTimer(PageTimer, 0, 20)` maybe could and also when its trying to get the details, if there is a delay for that, then the GUI will be frozen as long as the details is being fetched. – Delrius Euphoria Sep 09 '20 at 09:59
  • 1
    @CoolCloud, Thank you for your quick support Sir. You get it correctly, the timer gets freezes and the buttons created in the CheckRoutine becomes unresponsive till the time internet status isn't fetched. Is there any way I can fetch the details in parallel? – Krushi Sep 09 '20 at 10:11
  • 1
    Delete `inStatus.join()`. See [this post](https://stackoverflow.com/questions/16745507/tkinter-how-to-use-threads-to-preventing-main-event-loop-from-freezing) for details. – Henry Yik Sep 09 '20 at 10:15
  • 1
    @PriyaGarg please Try the fix suggested by Henry above. Addressing to your query on "Is there a way to fetch the details in parallel" - Yes. There is. You can try `multi processing / threading` where you create multiple threads to execute different tasks. [Read Here](https://docs.python.org/3/library/multiprocessing.html) to know about the concept. Moreover, the reason why the GUI gets frozen until the connection details are fetched is because the GUI doesn't update because program control does not reach the `mainloop` till the time connection request works out. This blocks the GUI. – P S Solanki Sep 09 '20 at 12:58
  • @HenryYik, Thank you sir for sharing the reference. I will give it a try today and will let you know If I get success or not. – Krushi Sep 10 '20 at 05:01
  • Henry Yik and P S Solanki, I tried the solution given in the link and it's working perfectly fine. Thank you for pointing me in right direction. – Krushi Sep 11 '20 at 05:02

0 Answers0