threading

Frank Millman frank at chagford.com
Thu Apr 10 05:17:50 EDT 2014


"Chris Angelico" <rosuav at gmail.com> wrote in message 
news:CAPTjJmq2xx_WG2ymCC0NNqisDO=DNnJhneGPiD3DE+xeiy5hjg at mail.gmail.com...
> On Thu, Apr 10, 2014 at 12:30 AM, Frank Millman <frank at chagford.com> 
> wrote:
>>
>>>
>>>> How does one distinguish betwen 'blocking' and 'non-blocking'? Is it
>>>> either/or, or is it some arbitrary timeout - if a handler returns 
>>>> within
>>>> that time it is non-blocking, but if it exceeds it it is blocking?
>>>
>>> No; a blocking request is one that waits until it has a response, and
>>> a non-blocking request is one that goes off and does something, and
>>> then comes back to you when it's done.
>>

Thanks for that clarification - I think I've got it now.

>>> def nonblocking_query(id):
>>>    print("Finding out who employee #%d is..."%id)
>>>    def nextstep(res):
>>>        print("Employee #%d is %s."%(id,res[0].name))
>>>    db.asyncquery(nextstep, "select name from emp where id=12345")
>>>
>>
>> In this example, what is 'db.asyncquery'?
>>
>> If you mean that you have a separate thread to handle database queries, 
>> and
>> you use a queue or other message-passing mechanism to hand it the query 
>> and
>> get the result, then I understand it. If not, can you explain in more
>> detail.
>
> It's an imaginary function that would send a request to the database,
> and then call some callback function when the result arrives. If the
> database connection is via a TCP/IP socket, that could be handled by
> writing the query to the socket, and then when data comes back from
> the socket, looking up the callback and calling it. There's no
> additional thread here.
>

I need some time to get my head around that, but meanwhile can you resolve 
this stumbling block?

The current version of my program uses HTTP. As I understand it, a client 
makes a connection and submits a request. The server processes the request 
and returns a result. The connection is then closed.

In this scenario, does async apply at all? There is no open connection to 
'select' or 'poll'. You have to ensure that the request handler does not 
block the entire process, so that the main loop is ready to accept more 
connections. But passing the request to a thread for handling seems an 
effective solution.

Am I missing something?

Frank






More information about the Python-list mailing list