Polling TCP server

I’m creating a custom component to integrate an alarm platform.
I can query the alarm system via tcp/ip to get the status of the the different sensors

import socket
import time
import binascii

try:
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
    print('Failed to create socket')

E_IP= "192.168.0.4"
E_PORT = 10002
client.connect((E_IP,E_PORT))

payload = bytes.fromhex( "02010800003f004803")

try:
    client.send(payload)
    msg = client.recv(4096)
    
except socket.error:
    print('Failed to send data')

client.close()

The component will need to poll the alarm system every 0.5 sec / 1sec.
What’s the best method to implement it? Can you point me to an existing component that is using this mechanism?

Thanks!

Maybe the pyscript integration?

I haven’t needed to do anything like what you describe, but I found this comment which seems like it may point you in the right direction.