Re: [Tutor] How to create memory backed file?
Kent, Thanks! I think that'll do it. I don't know what this list would do without you! -Modulok- On 12/27/09, Kent Johnson wrote: > On Sun, Dec 27, 2009 at 3:36 AM, Modulok wrote: >> List, >> >> How do I create a file which exists only in memory? (Think diskless.) >> >> I need to create an in-memory file. I need to pass this file, as a >> regular file object, to a subprocess. Unfortunately, the 'mmap' >> module's file-like object doesn't appear to work quite like a regular >> file would. (I simply assume that mmap would be the way to go. Other >> suggestions welcome!) That, or I'm doing something terribly wrong. > > How about using a pipe for stdin? This seems to work: > > import sys > from subprocess import Popen, PIPE > > fishes = "one fish\ntwo fish\nred fish\nblue fish\n" > > proc1 = Popen(['cat'], stdin=PIPE, stdout=sys.stdout, stderr=sys.stderr) > proc1.stdin.write(fishes) > proc1.stdin.close() > > Kent > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to create memory backed file?
On Sun, Dec 27, 2009 at 3:36 AM, Modulok wrote: > List, > > How do I create a file which exists only in memory? (Think diskless.) > > I need to create an in-memory file. I need to pass this file, as a > regular file object, to a subprocess. Unfortunately, the 'mmap' > module's file-like object doesn't appear to work quite like a regular > file would. (I simply assume that mmap would be the way to go. Other > suggestions welcome!) That, or I'm doing something terribly wrong. How about using a pipe for stdin? This seems to work: import sys from subprocess import Popen, PIPE fishes = "one fish\ntwo fish\nred fish\nblue fish\n" proc1 = Popen(['cat'], stdin=PIPE, stdout=sys.stdout, stderr=sys.stderr) proc1.stdin.write(fishes) proc1.stdin.close() Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to create memory backed file?
On 12/27/2009 12:03 PM Alan Gauld said... "Modulok" wrote How do I create a file which exists only in memory? (Think diskless.) The nearest I can think of is probably StringIO Take a look at the docs for that and see if it works for you... There's also http://docs.python.org/library/mmap.html -- but I've never tried it. Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to create memory backed file?
"Modulok" wrote How do I create a file which exists only in memory? (Think diskless.) The nearest I can think of is probably StringIO Take a look at the docs for that and see if it works for you... Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to create memory backed file?
List, How do I create a file which exists only in memory? (Think diskless.) I need to create an in-memory file. I need to pass this file, as a regular file object, to a subprocess. Unfortunately, the 'mmap' module's file-like object doesn't appear to work quite like a regular file would. (I simply assume that mmap would be the way to go. Other suggestions welcome!) That, or I'm doing something terribly wrong. For example: import mmap import sys from subprocess import Popen, PIPE fishes = "one fish\ntwo fish\nred fish\nblue fish\n" ### # With a real file, this works: ### fd = open('fish.txt', 'w') fd.write(fishes) fd = open('fish.txt', 'r') proc1 = Popen(['cat'], stdin=fd, stdout=sys.stdout, stderr=sys.stderr) ### # This doesn't (with mmap file): ### vfd = mmap.mmap(-1, len(fishes), mmap.MAP_PRIVATE) vfd.write(fishes) vfd.seek(0) proc2 = Popen(['cat'], stdin=vfd.fileno(), stdout=sys.stdout, stderr=sys.stderr) I just get the following error: Traceback (most recent call last): File "", line 1, in AttributeError: fileno I'm using Python 2.5, so I cannot use that slick 'SpooledTemporaryFile' method of the 'tempfile' module. The 'cat' is just a simple Unix system command for the purpose of illustration. Thanks! -Modulok- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor