[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: is there enough information?

exarkun

3/3/2008 1:12:00 PM

On Tue, 26 Feb 2008 21:45:24 -0800, Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:
> [snip]
>
> Threads, in Python, are good for parallel processing of items that
>tend to be I/O bound -- that is, stuff that blocks on lots of I/O calls
>allowing other threads to execute until they block too. Due to the GIL
>in the common Python implementation, threading is not useful for
>number-crunching (CPU bound) processing.
>
> Now, there is a very vocal group that recommend Twisted style
>asynchronous call-backs for everything in the world... But I think that
>group tends to forget that Windows I/O is incompatible with the
>low-level select() call often used to do parallel I/O -- leaving it only
>useful for the network socket I/O, but not local file I/O processing.

I'm not sure, but you seem to be implying that the only way to use Windows'
asynchronous I/O APIs is with threads. Actually, it is possible (and Twisted
allows you) to use these as well without writing a threaded application.

Perhaps you think it would be better to use them with threads, but that's
certainly not the _only_ way to use them as you implied.

Jean-Paul
7 Answers

Aaron Brady

3/3/2008 3:01:00 PM

0

On Mar 3, 7:11 am, Jean-Paul Calderone <exar...@divmod.com> wrote:
> On Tue, 26 Feb 2008 21:45:24 -0800, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:
>
> > [snip]
>
> >    Threads, in Python, are good for parallel processing of items that
> >tend to be I/O bound -- that is, stuff that blocks on lots of I/O calls
> >allowing other threads to execute until they block too. Due to the GIL
> >in the common Python implementation, threading is not useful for
> >number-crunching (CPU bound) processing.
>
> >    Now, there is a very vocal group that recommend Twisted style
> >asynchronous call-backs for everything in the world... But I think that
> >group tends to forget that Windows I/O is incompatible with the
> >low-level select() call often used to do parallel I/O -- leaving it only
> >useful for the network socket I/O, but not local file I/O processing.
>
> I'm not sure, but you seem to be implying that the only way to use Windows'
> asynchronous I/O APIs is with threads.  Actually, it is possible (and Twisted
> allows you) to use these as well without writing a threaded application.
>
> Perhaps you think it would be better to use them with threads, but that's
> certainly not the _only_ way to use them as you implied.
>
> Jean-Paul

What's the API call for it?

Dennis Lee Bieber

3/4/2008 4:35:00 AM

0

On Mon, 3 Mar 2008 07:00:55 -0800 (PST), castironpi@gmail.com declaimed
the following in comp.lang.python:

> What's the API call for it?

I'd suspect one of the win32event.WaitFor..., when combined with
win32file.CreateFile(), win32file.ReadFile() with the Overlapped flag
set, win32file.WriteFile() with Overlapped flag set...
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

Dennis Lee Bieber

3/4/2008 4:35:00 AM

0

On Mon, 3 Mar 2008 08:11:43 -0500, Jean-Paul Calderone
<exarkun@divmod.com> declaimed the following in comp.lang.python:

> I'm not sure, but you seem to be implying that the only way to use Windows'
> asynchronous I/O APIs is with threads. Actually, it is possible (and Twisted
> allows you) to use these as well without writing a threaded application.
>
I only pointed out that, on Windows, one can not use the common
/select()/ function with files. And one rarely sees examples of coding a
Twisted-style (emphasis on style) asynchronous callback system mixing
files and network sockes using the Windows-specific API.

If using threads, the Windows asynchronous I/O isn't needed... let
the thread block until the I/O completes, then transfer the data (or a
message that the data is available) back to the main processing
thread...
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

Aaron Brady

3/4/2008 6:27:00 PM

0

On Mar 3, 10:34 pm, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:
> On Mon, 3 Mar 2008 07:00:55 -0800 (PST), castiro...@gmail.com declaimed
> the following in comp.lang.python:
>
> > What's the API call for it?
>
>         I'd suspect one of the win32event.WaitFor..., when combined with
> win32file.CreateFile(), win32file.ReadFile() with the Overlapped flag
> set, win32file.WriteFile() with Overlapped flag set...
> --
>         Wulfraed        Dennis Lee Bieber               KD6MOG
>         wlfr...@ix.netcom.com              wulfr...@bestiaria.com
>                 HTTP://wlfraed.home.netcom.com/
>         (Bestiaria Support Staff:               web-a...@bestiaria.com)
>                 HTTP://www.bestiaria.com/

select maps to two different calls on Windows.

David Bolen

3/4/2008 11:59:00 PM

0

Dennis Lee Bieber <wlfraed@ix.netcom.com> writes:

> On Mon, 3 Mar 2008 08:11:43 -0500, Jean-Paul Calderone
> <exarkun@divmod.com> declaimed the following in comp.lang.python:
>
>> I'm not sure, but you seem to be implying that the only way to use Windows'
>> asynchronous I/O APIs is with threads. Actually, it is possible (and Twisted
>> allows you) to use these as well without writing a threaded application.
>>
> I only pointed out that, on Windows, one can not use the common
> /select()/ function with files. And one rarely sees examples of coding a
> Twisted-style (emphasis on style) asynchronous callback system mixing
> files and network sockes using the Windows-specific API.
>
> If using threads, the Windows asynchronous I/O isn't needed... let
> the thread block until the I/O completes, then transfer the data (or a
> message that the data is available) back to the main processing
> thread...

You're probably right that it's rare, but when needed, using the
Windows asynchronous/overlapping API can provide a better solution
than blocking threads depending on the needs at hand, and without
involving any callbacks or Twisted-style programming.

An example of mine is high performance serial port handling as part of
a custom FHSS wireless adapter with a serial port interface to the PC.
In this case, minimizing I/O latency was crucial since delays could
mean missing a broadcast timeslot (about 15ms) on the wireless
network. A serial port isn't a disk file, but certainly a "file" in
the context of Windows handles.

Early implementations used independent threads for reading/writing to
the serial port and blocking during such operations, but that turned
out to have an undesirable amount of latency, and was also difficult
to interrupt when the threads were in a blocked condition.

Instead I created a single thread that had a loop using overlapped I/O
simultaneously in each direction as well as native Windows event
objects for aborting or signaling that there was additional data to be
written (the pending read I/O handled the read case). The main loop
was just a WaitForMultipleObjects to handle any of the I/O completion
indications, requests for more I/O or aborts. It was very high
performing (low latency) with low CPU usage - measurably better than a
multi-threaded version.

Communication with the rest of the application was through a
thread-safe bi-directional buffer object, also using native Win32
event objects. It worked similar to a queue, but by using the native
event objects I didn't have the performance inefficiencies for reads
with timeouts of the Python objects. The underlying Python primitives
don't have the timeout capability built in, so reads with timeouts get
implemented through checks for data interspersed with increasing
sleeps, which adds unnecessary latency.

Anyway, it worked extremely well, and was a much better fit for my
needs than a multi-threaded version with blocking I/O, without it
having to be Twisted-style.

-- David

Aaron Brady

3/5/2008 4:10:00 AM

0

On Mar 4, 5:59 pm, David Bolen <db3l....@gmail.com> wrote:
> Dennis Lee Bieber <wlfr...@ix.netcom.com> writes:
>
>
>
>
>
> > On Mon, 3 Mar 2008 08:11:43 -0500, Jean-Paul Calderone
> > <exar...@divmod.com> declaimed the following in comp.lang.python:
>
> >> I'm not sure, but you seem to be implying that the only way to use Windows'
> >> asynchronous I/O APIs is with threads.  Actually, it is possible (and Twisted
> >> allows you) to use these as well without writing a threaded application.
>
> >    I only pointed out that, on Windows, one can not use the common
> > /select()/ function with files. And one rarely sees examples of coding a
> > Twisted-style (emphasis on style) asynchronous callback system mixing
> > files and network sockes using the Windows-specific API.
>
> >    If using threads, the Windows asynchronous I/O isn't needed... let
> > the thread block until the I/O completes, then transfer the data (or a
> > message that the data is available) back to the main processing
> > thread...
>
> You're probably right that it's rare, but when needed, using the
> Windows asynchronous/overlapping API can provide a better solution
> than blocking threads depending on the needs at hand, and without
> involving any callbacks or Twisted-style programming.
>
> An example of mine is high performance serial port handling as part of
> a custom FHSS wireless adapter with a serial port interface to the PC.
> In this case, minimizing I/O latency was crucial since delays could
> mean missing a broadcast timeslot (about 15ms) on the wireless
> network.  A serial port isn't a disk file, but certainly a "file" in
> the context of Windows handles.
>
> Early implementations used independent threads for reading/writing to
> the serial port and blocking during such operations, but that turned
> out to have an undesirable amount of latency, and was also difficult
> to interrupt when the threads were in a blocked condition.
>
> Instead I created a single thread that had a loop using overlapped I/O
> simultaneously in each direction as well as native Windows event
> objects for aborting or signaling that there was additional data to be
> written (the pending read I/O handled the read case).  The main loop
> was just a WaitForMultipleObjects to handle any of the I/O completion
> indications, requests for more I/O or aborts.  It was very high
> performing (low latency) with low CPU usage - measurably better than a
> multi-threaded version.
>
> Communication with the rest of the application was through a
> thread-safe bi-directional buffer object, also using native Win32
> event objects.  It worked similar to a queue, but by using the native
> event objects I didn't have the performance inefficiencies for reads
> with timeouts of the Python objects.  The underlying Python primitives
> don't have the timeout capability built in, so reads with timeouts get
> implemented through checks for data interspersed with increasing
> sleeps, which adds unnecessary latency.
>
> Anyway, it worked extremely well, and was a much better fit for my
> needs than a multi-threaded version with blocking I/O, without it
> having to be Twisted-style.
>
> -- David- Hide quoted text -
>
> - Show quoted text -

How does it work? From reading threading.py, _Condition.wait()
acquires self.lock() too many times-- that is, once to call wait
("cannot wait on un-aquired lock"), and once after--- are "all
waiters" waiting back at self.acquire, just to make it to
self.notify... and only one at a time at that!? Don't waiters have to
release before another waiter can go?

Aaron Brady

3/5/2008 4:12:00 AM

0

> How does it work?  From reading threading.py, _Condition.wait()
> acquires self.lock() too many times-- that is, once to call wait
> ("cannot wait on un-aquired lock"), and once after--- are "all
> waiters" waiting back at self.acquire, just to make it to
> self.notify... and only one at a time at that!?  Don't waiters have to
> release before another waiter can go?

It's not like Condition().acquire() appends self._lock to
self._waiters *before* delegationing! So... how?