Waiting for an event and for a time.sleep, possible?


I’m using socket.accept()

This is blocking, until a client connect, ok.

Would it possible to wait for a connection and in the same time waiting a certain amount of time to launch something else ?

With asyncio or anything else (that have a faster learning curve…)


Hi! The socket module is not asyncio-related, so I guess this is in the wrong category.

Anyway, since accept() does not appear to have a timeout argument, maybe you should look at the select or selectors module.

Thanks @fonini ,

I don’t want to timeout socket.accept() I want to run in “parallel” a sleep

So while the program wait for a connection, if a separate timer , timeout an other function is called, while still waiting for socket.accept()

You can’t; you need to use threading or multiprocessing.

Thank you @elis.byberi
I’ve already use threading so I will see If I go further with it (and catch back a result for example)

You can do that using asyncio, but not in parallel!
You can schedule a task using call_later:

import asyncio

async def my_task():
    print("Task is running...")

async def main():
    print("Main program started.")
    loop = asyncio.get_event_loop()
    loop.call_later(3, asyncio.create_task, my_task())
    print("Task scheduled to run after 3 seconds.")
    await asyncio.sleep(4)  # Ensure the program doesn't exit before the scheduled task completes
    print("Main program completed.")

# Run the main function

I expect you can do this with async.

At least with the twisted async framework you can and I used it all the time.

As I recall the listening socket is set non-blocking and then the event loop waits for it say it can be read.
To handle the read you call accept() that will not block.
For the timeout use the async framework’s timer mechanism.

1 Like

Yep, or with select() if you want a lower-level way to do the same thing.

1 Like

My response was to this post:

I provided more detail in my previous post, suggesting the utilization of asyncio to execute them concurrently rather than in parallel.

I didn’t mention anything about the coroutine loop.sock_accept(sock), leaving it as an exercise.

I mentioned it in another thread, but unfortunately, it didn’t provide much clarity. The original poster became even more confused. 1 2

One other way to emulate this is to use a timeout on the socket to periodically try an accept() and do something else in between, etc.

That way it also wouldn’t wait forever if a peer never connects.

Ref: python socket object accept time out - Stack Overflow

The whole point of the async franeworks is that you can have complete control and not need to have approximate/emulated solutions.

If you’re comfortable working with low-level interfaces, timerfd is available in Python 3.13:

The link to the Debian man page in see also: os — Miscellaneous operating system interfaces — Python 3.13.0b2 documentation is broken.
Also, it would be nice to have a link from the HOWTO page to the relevant library entry. Links to epoll and select exist, but not timerfd_create.

1 Like