Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I use Python's asyncio library to create a server that can handle telnet requests. Because asyncio's server loop is single threaded, I thought it would make more sense that when asyncio tells me that I got data (asyncio.Protocol.data_received) to simply schedule a task, rather than block receiving further data until I finish handling this bit of data and transmitting my reply.

But because the only I/O operation that I do with this data is transmitting a reply or sending things to other clients, I am not sure there is a reason to not just block the single thread until done. If it is in any way possible for the asyncio loop to to use more than one thread, then if I schedule tasks, they will actually be able to be performed in parallel, but I don't think this is the case.

I considered that maybe for locking purposes I would prefer using coroutines, but if I am single threaded locking shouldn't be needed.

share|improve this question
    
If it is in any way possible for the asyncio loop to to use more than one thread, then if I schedule tasks, they will actually be able to be performed in parallel, but I don't think this is the case. -- That's too bad. There's really only so much you can do with a single thread. The whole point of async is for your server loop to return immediately so that it can handle more requests, and it can't do that if it has to block. – Robert Harvey Nov 12 at 15:53
    
I am inexperienced with Python and asyncio, so I was hoping that I was missing something vital. – gil_bz Nov 12 at 16:11
    
Can you write threads in ordinary Python, without asyncio? You ought to be able to do anything in asyncio that you can do in the language without it. It's not about locking, unless you're actively sharing data between threads. It might be about database locking. – Robert Harvey Nov 12 at 16:13
    
It seems to me that if I really want to improve this, I should have one thread with the server, and another thread that handles the requests (with the server thread queuing those requests). But I am concerned that this won't improve performance much because of the GIL. – gil_bz Nov 12 at 19:05
    
Only one way to find out. – Robert Harvey Nov 12 at 19:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.