'Check internet conection status in background python3

In python3 (debina) im try to check connection status like this:

import urllib.request


def connect_status(host):
    print(host)
    print("checking connection")
    try:
        #window.after(5000,hide_big) 
        urllib.request.urlopen(host, timeout = 10)
        print("Online")
        return True
            
    except:
        print("Offline")
        return False

I run in in main "script"

def connect(host='http://google.com'):
    window.after(20000, connect)
    if con_stat.connect_status(host) is True:
        window.after(5000,hide_big)
        int_con_status.set("Online")
        #print("Online")
        pop_up_big_message.set("ONLINE")
        pop_up_message.set("ONLINE")
        led_green.off()
        led_red.off()
        led_blue.on()
        #show_big()
    else:
        int_con_status.set("Offline")
        #print("Offline")
        pop_up_big_message.set("OFFLINE")
        pop_up_message.set("OFFLINE")
        show_big()
        led_green.off()
        led_red.on()
        led_blue.off()

It works.

The problem is that this code pauses the main program until it finishes. How do I do this in the background?

All I have to do is check the status from time to time and save it in a variable.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source