[Twisted-Python] Asynchronous File I/O

Jp Calderone exarkun at divmod.com
Tue Jun 21 10:42:39 MDT 2005


On Tue, 21 Jun 2005 19:16:20 +0300, Mustafa Sakalsiz <mustafa at liqia.com> wrote:
>Is there a way to read and writes files asynchronously using twisted.
>
>The files may be of regular files or devices (/dev/*).
>

Twisted doesn't wrap or otherwise expose any platform specific asynchronous disk or device file IO routines.

Twisted's KQueue and IOCP support could be extended to provide this feature on BSD and Win32 (and AIO could probably be used to implement the same API on Linux), if there is some compelling use case for this.

Most applications get along fine using blocking disk IO, since it generally doesn't block for very long.  For cases where wrapping a real asynchronous IO layer is too much work but doing blocking disk IO is too detrimental, simply sending IO operations to a thread may be a suitable solution.  For example, twisted.internet.threads.deferToThread(fileObj.read, size) returns a Deferred that fires with at most size bytes from fileObj.  If you go this route, you must be careful to serialize operations on individual file objects, of course.

For device files, inotify.py in the Twisted sandbox has some code that might be useful (OTOH, perhaps it is just overengineered).  See its connectCharacterDevice() function.

Jp




More information about the Twisted-Python mailing list