[Tutor] file object in memory
Martin Walsh
mwalsh at groktech.org
Mon Jul 7 02:16:02 CEST 2008
Monika Jisswel wrote:
> You know bob, you are very clever, I have used RAM disk for realtime
> recording of audio before but it never occured to me to use it for light
> jobs like this one, I just compeletely ignored it as an option & by the
> way this openes a lot of doors for many of my other programs.
>
> but wouldn't it be very nice if there was a way of creating a file
> object inside python ? something like :
> myfile = mkfile('some_string')
> & we're done ?
If I understand what you're after, perhaps tempfile.NamedTemporaryFile
is close. Granted it creates a 'real' file, subject to filesystem
performance, but it is automatically deleted when closed.
---
Also, while I wouldn't recommend it, I thought it might be worth
mentioning that you can specify a named pipe (or fifo) with mysql 'load
data' -- although this may be limited to *nix or Mac, I'm not sure.
It is a tricky dance since, IIUC, the mysql process has to open the
named pipe for reading before you can write to it, else the write will
block waiting for a reader and your program will hang. My first thought
would be to spawn a subprocess of the mysql client, or mysqlimport, to
start the read, then write data to the named pipe. Something like the
following (untested):
...
fifoname = '/tmp/mysql.fifo'
if os.path.exists(fifoname):
os.remove(fifoname)
os.mkfifo(fifoname)
sql = """\
"load data local infile '%s' into table statistics
fields terminated by '\\t' lines terminated by '\\n';"
""" % fifoname
conninfo[sql] = sql
mysqlcmd = \
'mysql %(dbname)s -h %(host)s -u %(user)s -e %(sql)s' % conninfo
mysqlp = sp.Popen(mysqlcmd, stdout=sp.PIPE, shell=True)
# should be ready to write
fifofp = file(fifoname, 'w')
p1 = sp.Popen(somecmd1, stdout=fifofp, shell=True)
fifofp.close()
mysqlp.wait()
...
Seems like an awful lot of work, for questionable gain. I would use a
regular file. If you're concerned about performance, Bob's ramdisk
suggestion seems more appropriate.
HTH,
Marty
>
>
> I don't think there is a pure Pythonic way. Have you considered
> using a RamDisk? http://en.wikipedia.org/wiki/RAM_disk for general
> details.
>
> http://www.codeguru.com/cpp/w-p/system/devicedriverdevelopment/article.php/c5789/
> for a Windows Ram Disk driver installer. I just tried it - it works
> like a charm.
>
> HTH.
>
> --
> Bob Gailer
> 919-636-4239 Chapel Hill, NC
More information about the Tutor
mailing list