O_DIRECT on stdin?

Dan Stromberg strombrg at dcs.nac.uci.edu
Mon Nov 7 15:29:05 EST 2005


On Mon, 07 Nov 2005 19:48:47 +0000, Gordon Burditt wrote:

>> [quoted text muted]
> 
> There is no O_DIRECT or fcntl() in Standard C.
> 
> fcntl() operates on an open file descriptor, and the file descriptor
> for stdin is 0.  Two calls to fcntl(), one with F_GETFL and one
> with F_SETFL, would do what you want.
> 
> I'm not sure why you want to do that, though.  It's not going to
> get you character-at-a-time I/O, if that's what you want.
> 
> 						Gordon L. Burditt

I want to be able to read a HUGE file without having such a negative
impact on the system's buffer cache.

I'm trying:

 	if hasattr(os, 'O_DIRECT'):
		try:
			flags = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)      
			flags |= os.O_DIRECT
			fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, flags)
		except:
			sys.stderr.write('Setting O_DIRECT on stdin attempted but failed\n')
		else:
			sys.stderr.write('Setting O_DIRECT on stdin succeeded :)\n')

...but while this code doesn't error out, I get:

seki-root> reblock -e $[1024*1024*80] $[1024*1024] 300 < /dev/sda1 > /dev/null
stdin seems seekable, but file length is 0 - no exact percentages
Estimated filetransfer size is 85899345920 bytes
Estimated percentages will only be as accurate as your size estimate
Setting O_DIRECT on stdin succeeded :)
Traceback (most recent call last):
  File "/Dcs/seki/strombrg/bin/reblock", line 276, in ?
    main()
  File "/Dcs/seki/strombrg/bin/reblock", line 222, in main
    block = os.read(0,blocksize)
OSError: [Errno 22] Invalid argument
Mon Nov 07 12:25:53

...but if I comment out the fcntl/O_DIRECT code, then the same thing works
well.

Any other ideas folks?

Thanks!




More information about the Python-list mailing list