Receiving sockets blocking async?

Viewed 74

The following code prints 1 every second and also tries to recieve udp packets from 244.0.2.60:4445

import socket
import struct
import asyncio

multicast_group = '224.0.2.60'
server_address = ('', 4445)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(server_address)
group = socket.inet_aton(multicast_group)
mreq = struct.pack('4sL', group, socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

async def check_server():
    while True:
        print(1)
        await asyncio.sleep(1)
            

async def tag_server():
    while True:
        data, address = sock.recvfrom(1024)
        data = data.decode("utf-8")
        print(data, address)

# Create tasks
async def main():
    await asyncio.gather(check_server(),tag_server())

# Run
asyncio.run(main())

I expect it to return the following way

1
1 # print 1 every second
1
1
b'packet' # receives packet
1
1
1
b'packet'
1
1

What I get is this :

1

Why does receiving packets block my program? What can I do to fix this?

0 Answers
Related