Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #69956

Re: threading

References (4 earlier) <mailman.8986.1396914468.18130.python-list@python.org> <877g70wg8p.fsf@elektro.pacujo.net> <li3hki$grv$1@ger.gmane.org> <CAPTjJmqwhb8O8vq84mMTv+-Rkc3Ff1AQDXe5cs8Y5gY02kHyNg@mail.gmail.com> <li3lho$vpr$1@ger.gmane.org>
Date 2014-04-10 00:44 +1000
Subject Re: threading
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.9079.1397054695.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Apr 10, 2014 at 12:30 AM, Frank Millman <frank@chagford.com> wrote:
>
> "Chris Angelico" <rosuav@gmail.com> wrote in message
> news:CAPTjJmqwhb8O8vq84mMTv+-Rkc3Ff1AQDXe5cs8Y5gY02kHyNg@mail.gmail.com...
>> On Wed, Apr 9, 2014 at 11:23 PM, Frank Millman <frank@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.
>
> Does reading from disk count as blocking? Strictly speaking I would have
> thought 'yes'.

What the operation actually *is* is quite immaterial. Reading from the
disk can be done as blocking or non-blocking; if you simply open a
file and call its read() method, that'll block by default (the open()
call can block, too, but you asked about reading). When you ask for
something to be done and expect a return value with the result, that
implies a blocking operation. Even simple work like floating-point
addition can be blocking or nonblocking; I learned some of the basics
of the 8087 coprocessor and how to do assembly-language floating
point, and it was, by default, a non-blocking operation - you say "Go
do this", and then you say "FWAIT" to block until the coprocessor is
done. But normally you want to be able to write this:

a = 12.3
b = 45.6
c = a + b
print("The sum is:",c)

rather than this:

add_async(a, b, lambda c: print("The sum is:",c))

> In other words, non-blocking implies that everything required to pass off
> the request to a handler and be ready to deal with the next one must already
> be in memory, and it must not rely on communicating with any outside
> resource at all. Is this correct?

Nope. It simply requires that communicating with an outside resource
be done asynchronously. You fire off the request and either check for
its completion periodically (polling) or get a notification when it's
done (callback, usually).

>> 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.

> If I have understood correctly, then is there any benefit at all in my going
> async? I might as well just stick with threads for the request handling as
> well as the database handling.

Threads are a convenient way to handle things, but they're an
alternative. You generally do one or the other. Occasionally you'll
use a bit of both, maybe because your database handler can't work
asynchronously, but ideally you shouldn't need to mix and match like
that.

I'm oversimplifying horribly, here, but hopefully helpfully :)

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: threading Ben Finney <ben+python@benfinney.id.au> - 2014-04-07 13:05 +1000
  Re: threading Roy Smith <roy@panix.com> - 2014-04-06 23:48 -0400
    Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-07 13:56 +1000
      Re: threading Roy Smith <roy@panix.com> - 2014-04-07 08:26 -0400
        Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-07 22:34 +1000
          Re: threading Roy Smith <roy@panix.com> - 2014-04-07 09:22 -0400
            Re: threading Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-04-07 14:41 +0100
            Re: threading Marko Rauhamaa <marko@pacujo.net> - 2014-04-07 16:49 +0300
              Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-08 00:27 +1000
                Re: threading Marko Rauhamaa <marko@pacujo.net> - 2014-04-07 17:51 +0300
                Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-08 01:12 +1000
            Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-08 00:24 +1000
      Re: threading Rick Johnson <rantingrickjohnson@gmail.com> - 2014-04-08 18:09 -0700
        Re: threading "Neil D. Cerutti" <neilc@norwich.edu> - 2014-04-09 09:50 -0400
          Re: threading Rick Johnson <rantingrickjohnson@gmail.com> - 2014-04-09 08:51 -0700
            Re: threading MRAB <python@mrabarnett.plus.com> - 2014-04-09 18:47 +0100
              Re: threading Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-04-10 11:35 +1200
                Re: threading Roy Smith <roy@panix.com> - 2014-04-09 19:53 -0400
                Re: threading Andrew Berg <robotsondrugs@gmail.com> - 2014-04-09 19:02 -0500
                Re: threading Steven D'Aprano <steve@pearwood.info> - 2014-04-10 02:43 +0000
                Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-10 13:08 +1000
                Re: threading Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-04-10 09:23 +0100
                Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-10 19:11 +1000
            Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-10 04:00 +1000
            Re: threading Steven D'Aprano <steve@pearwood.info> - 2014-04-10 03:44 +0000
              Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-10 13:54 +1000
    Re: threading Ben Finney <ben+python@benfinney.id.au> - 2014-04-07 15:22 +1000
    Re: threading Ethan Furman <ethan@stoneleaf.us> - 2014-04-08 11:09 -0700
    Re: threading Sturla Molden <sturla.molden@gmail.com> - 2014-04-08 21:41 +0200
      Re: threading Grant Edwards <invalid@invalid.invalid> - 2014-04-08 20:30 +0000
        Re: threading Sturla Molden <sturla.molden@gmail.com> - 2014-04-09 00:32 +0200
          Re: threading Rustom Mody <rustompmody@gmail.com> - 2014-04-08 19:17 -0700
  Re: threading Marko Rauhamaa <marko@pacujo.net> - 2014-04-07 08:10 +0300
    Re: threading Paul Rubin <no.email@nospam.invalid> - 2014-04-06 22:39 -0700
      Re: threading Marko Rauhamaa <marko@pacujo.net> - 2014-04-07 08:46 +0300
      Re: threading Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-04-07 19:47 -0400
        Re: threading Marko Rauhamaa <marko@pacujo.net> - 2014-04-08 08:19 +0300
          Re: threading Sturla Molden <sturla.molden@gmail.com> - 2014-04-08 10:47 +0000
            Re: threading Marko Rauhamaa <marko@pacujo.net> - 2014-04-08 15:10 +0300
              Re: threading Sturla Molden <sturla.molden@gmail.com> - 2014-04-08 16:37 +0000
                Re: threading Marko Rauhamaa <marko@pacujo.net> - 2014-04-08 20:17 +0300
            Re: threading Roy Smith <roy@panix.com> - 2014-04-08 09:19 -0400
              Re: threading Sturla Molden <sturla.molden@gmail.com> - 2014-04-08 15:44 +0000
                Re: threading Paul Rubin <no.email@nospam.invalid> - 2014-04-08 09:38 -0700
                Re: threading Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-04-09 14:42 +0100
          Re: threading "Frank Millman" <frank@chagford.com> - 2014-04-09 15:23 +0200
            Re: threading Marko Rauhamaa <marko@pacujo.net> - 2014-04-09 16:55 +0300
              Re: threading "Frank Millman" <frank@chagford.com> - 2014-04-09 16:46 +0200
                Re: threading Marko Rauhamaa <marko@pacujo.net> - 2014-04-09 20:31 +0300
                Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-10 03:52 +1000
                Re: threading Mark H Harris <harrismh777@gmail.com> - 2014-04-10 08:29 -0500
                Re: threading Sturla Molden <sturla.molden@gmail.com> - 2014-04-09 19:20 +0000
          Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-09 23:47 +1000
            Re: threading Roy Smith <roy@panix.com> - 2014-04-09 10:44 -0400
          Re: threading "Frank Millman" <frank@chagford.com> - 2014-04-09 16:30 +0200
            Re: threading Roy Smith <roy@panix.com> - 2014-04-09 10:52 -0400
              Re: threading Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-04-10 11:19 +1200
            Re: threading Marko Rauhamaa <marko@pacujo.net> - 2014-04-09 19:48 +0300
          Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-10 00:44 +1000
          Re: threading Sturla Molden <sturla.molden@gmail.com> - 2014-04-09 15:29 +0000
          Re: threading Terry Reedy <tjreedy@udel.edu> - 2014-04-09 12:14 -0400
          Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-10 02:25 +1000
          Re: threading Sturla Molden <sturla.molden@gmail.com> - 2014-04-09 16:32 +0000
          Re: threading Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-04-09 19:44 -0400
          Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-10 11:05 +1000
          Re: threading "Frank Millman" <frank@chagford.com> - 2014-04-10 11:17 +0200
          Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-10 19:40 +1000
          Re: threading "Frank Millman" <frank@chagford.com> - 2014-04-10 13:10 +0200
            Re: threading Marko Rauhamaa <marko@pacujo.net> - 2014-04-10 14:43 +0300
              Re: threading Roy Smith <roy@panix.com> - 2014-04-10 08:56 -0400
              Re: threading Sturla Molden <sturla.molden@gmail.com> - 2014-04-10 15:24 +0000
                Re: threading Marko Rauhamaa <marko@pacujo.net> - 2014-04-10 19:20 +0300
              Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-11 01:32 +1000
                Re: threading Marko Rauhamaa <marko@pacujo.net> - 2014-04-10 19:25 +0300
                Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-11 03:08 +1000
                Re: threading Rustom Mody <rustompmody@gmail.com> - 2014-04-10 11:14 -0700
                Re: threading Marko Rauhamaa <marko@pacujo.net> - 2014-04-10 22:44 +0300
                Re: threading Rustom Mody <rustompmody@gmail.com> - 2014-04-10 13:21 -0700
                Re: threading Marko Rauhamaa <marko@pacujo.net> - 2014-04-10 23:44 +0300
                Re: threading Rustom Mody <rustompmody@gmail.com> - 2014-04-10 22:15 -0700
                Re: threading Rustom Mody <rustompmody@gmail.com> - 2014-04-10 23:50 -0700
                Re: threading Marko Rauhamaa <marko@pacujo.net> - 2014-04-11 18:36 +0300
                Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-12 01:53 +1000
                Re: threading Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-04-11 16:58 +0100
                Re: threading Rustom Mody <rustompmody@gmail.com> - 2014-04-11 11:54 -0700
                Re: threading Marko Rauhamaa <marko@pacujo.net> - 2014-04-11 22:27 +0300
                Re: threading Sturla Molden <sturla.molden@gmail.com> - 2014-04-11 01:51 +0200
                Re: threading Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-04-11 05:35 +0000
                Re: threading Sturla Molden <sturla.molden@gmail.com> - 2014-04-11 09:26 +0000
                Re: threading Roy Smith <roy@panix.com> - 2014-04-11 08:36 -0400
                Re: threading Grant Edwards <invalid@invalid.invalid> - 2014-04-11 16:18 +0000
                Re: threading Sturla Molden <sturla.molden@gmail.com> - 2014-04-11 02:21 +0200
                Re: threading Terry Reedy <tjreedy@udel.edu> - 2014-04-10 20:23 -0400
          Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-10 21:19 +1000
      Re: threading Sturla Molden <sturla.molden@gmail.com> - 2014-04-08 02:06 +0000
        Re: threading alister <alister.nospam.ware@ntlworld.com> - 2014-04-08 11:07 +0000
          Re: threading Roy Smith <roy@panix.com> - 2014-04-08 09:13 -0400
            Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-08 23:23 +1000
              Re: threading alister <alister.nospam.ware@ntlworld.com> - 2014-04-08 14:15 +0000
                Re: threading Sturla Molden <sturla.molden@gmail.com> - 2014-04-08 16:06 +0000
            Re: threading Sturla Molden <sturla.molden@gmail.com> - 2014-04-08 15:40 +0000
              Re: threading Paul Rubin <no.email@nospam.invalid> - 2014-04-08 09:46 -0700
                Re: threading Chris Angelico <rosuav@gmail.com> - 2014-04-09 02:46 +1000
                Re: threading Sturla Molden <sturla.molden@gmail.com> - 2014-04-08 17:17 +0000
          Re: threading Sturla Molden <sturla.molden@gmail.com> - 2014-04-08 15:19 +0000

csiph-web