[python-win32] How to use win32file.TransmitFile with non blockingsockets?

Giampaolo Rodolà g.rodola at gmail.com
Fri Jan 28 14:38:19 CET 2011


Point of asynchronous programming is that all function calls should
return immediately.
"wait for an async operation to complete" sounds kinda wrong to me.


--- Giampaolo
http://code.google.com/p/pyftpdlib/
http://code.google.com/p/psutil/


2011/1/28 Roger Upole <rupole at hotmail.com>:
> Giampaolo Rodolà wrote:
>> Hi all,
>> I'm trying to take advantage of TransmitFile function in pyftpdlib:
>> http://code.google.com/p/pyftpdlib/issues/detail?id=152
>>
>> I've noticed pywin32 provides a wrapper:
>> http://sourceforge.net/tracker/index.php?func=detail&aid=1962146&group_id=78018&atid=551956
>> The example shown in there, though, assumes the socket is in blocking
>> state and the file gets sent entirely in a unique call.
>> I need to adapt that example to make it work with non-blocking sockets.
>> Specifically I want TransmitFile() and GetOverlappedResult() to return
>> immediately reporting the number of bytes sent.
>> The code below is what I managed to come up with so far but it
>> obviously doesn't work.
>> It either fails with "error: (996, 'GetOverlappedResult', 'Overlapped
>> I/O event is not in a signaled state.')" or the file delivered is
>> corrupted.
>> I don't have a clue on how should I use the overllaped structure,
>> neither it's clear to me what should I do to detect when EOF is
>> reached.
>>
>> Any hint?
>>
>>       def sendfile(sock, file, offset, nbytes):
>>           """Windows TranmistFile() wrapper, adapted to look like
>>           UNIX sendfile().
>>           """
>>           ol = pywintypes.OVERLAPPED()
>>           ol.Offset = offset
>>           ol.hEvent = win32event.CreateEvent(None, 0, 0, None)
>>           filehandle = win32file._get_osfhandle(file)
>>           win32file.TransmitFile(sock, filehandle, nbytes, offset, ol, 0)
>>           try:
>>               sent = win32file.GetOverlappedResult(filehandle, ol, 0)
>>           except pywintypes.error, err:
>>               if err.args[0] == 38:  # EOF
>>                   return 0
>>               raise
>>           else:
>>               return sent
>>
>>
>> Thanks in advance,
>>
>> --- Giampaolo
>
> The third arg to GetOverlappedResult should be True to wait for an
> async operation to complete.
>
>          Roger
>
>
>
>
>
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32
>
>


More information about the python-win32 mailing list