Python Forum
Timer for Python that doesn't interrupt the program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Timer for Python that doesn't interrupt the program
#1
I have to implement the AIMD protocol where I will send the quantity of packets of my sliding window to other server, and while I don't lose any packet in the communication between a client and a server or the timer doesn't run out I will be increasing my sliding window.

I need to create a timer that resets my slidingWindowSize variable without interrupting the application from running, but I am facing difficulties with this timer, because everything that I have tried interrupted the application.

I developed the UDP server and UDP client that you can see below:
# Server
    import socket
    import sys
    import config
    
    MY_IP = config.myIP
    OTHER_IP = config.otherIp
    PORT_NUMBER = config.port_number
    
    # Create a UDP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    
    # Bind the socket to the port
    server_address = (MY_IP, PORT_NUMBER)
    print('starting up on {} port {}'.format(*server_address))
    sock.bind(server_address)
    
    while True:
        print('\nwaiting to receive message')
        data, address = sock.recvfrom(4097)
    
        print('received {} bytes from {}'.format(
            len(data), address))
        print(data)
    
        if data:
            sent = sock.sendto(data, address)
            print('sent {} bytes back to {}'.format(
                sent, address))
    # Client
    import socket
    import sys
    import config # My computer IP
    import sched
    import time
    
    from threading import Timer
    
    MY_IP = config.myIP # My IP 
    PORT_NUMBER = config.port_number # Port Number
    
    def timeout(slidingWindowSize):
        print('Window size value after:', time.time(), slidingWindowSize)
        exit()
    
    scheduler = sched.scheduler(time.time, time.sleep)
    
    # Create a UDP socket with my IP and Port
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    server_address = (MY_IP, PORT_NUMBER)
    slidingWindowSize = 1
    
    while True:
        print("Window size value before: ", slidingWindowSize)
        if slidingWindowSize == 128:
            exit()
        for i in range(slidingWindowSize):
            packet = bytes([1])
            # send data
            print('sending {!r}'.format(packet))
            sent = sock.sendto(packet, server_address)
    
        # Here I would need the timer to start, but it interrupts my algorithm.
        # (I have tried sched, but it didn't work)
        print('START:', time.time())
        scheduler.enter(3, 1, timeout, (slidingWindowSize,))
        #scheduler.run() -> tried to use it but it interrupted the algorithm
    
        for j in range(slidingWindowSize):
            # Receive response
            print('waiting to receive')
            data, server = sock.recvfrom(4097)
            print('received {!r}'.format(data))
    
        slidingWindowSize = slidingWindowSize * 2
        time.sleep(2)
    
    sock.close()
What should I change in my code so that the timer won't stop the application from working and when it reaches timeout it sets slidingWindowSize to 1 again?

Yes it will run forever..., but that is the objective, to analize the packet loss.

I am running the application on Windows 10 and Kubuntu, Python versions 3.8.0.

Thanks!
Reply
#2
Why not use a threading.Timer instance?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020