Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > creating a block file for file-like object

Reply
Thread Tools

creating a block file for file-like object

 
 
Iain
Guest
Posts: n/a
 
      11-07-2008
Hi,

I have a function that only accepts filenames, rather than file-like
objects (because its a wrapper around a C++ function, I think).

I want to feed some potentially large "files" into this function, but
they are coming in as streams (eg from a url) and so are only
represented in my code as file-like objects.

Can someone give me some pointers as to how I might create some sort
of blocking device file or named pipe or something that will give this
stream a filename without having to actually write the data to disk
and then read it back in before deleting it?

I've sort of tried the FIFO thing, but I think I'm getting caught by
its blocking behaviour on open so as soon as I try to open the named
pipe (whether for reading or writing) my script just hangs.

Any help would be appreciated.

Cheers
Iain
 
Reply With Quote
 
 
 
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      11-07-2008
In message
<e424a5ea-0a62-4ca1-842b->, Iain
wrote:

> Can someone give me some pointers as to how I might create some sort
> of blocking device file or named pipe ...


mkfifo /path/to/named/pipe
 
Reply With Quote
 
 
 
 
Iain
Guest
Posts: n/a
 
      11-08-2008
On Nov 7, 4:42*pm, Lawrence D'Oliveiro <l...@geek-
central.gen.new_zealand> wrote:
> In message
> <e424a5ea-0a62-4ca1-842b-cb2cc2cea...@n33g2000pri.googlegroups.com>, Iain
> wrote:
>
> > Can someone give me some pointers as to how I might create some sort
> > of blocking device file or named pipe ...

>
> * * mkfifo /path/to/named/pipe


Thanks.

I did get that far myself with os.mkfifo - my problem is actually
using it. To me it seemed like I wanted to do something like

streamobj = urllib.urlopen("http://whereever.com/file")
fifoobj = open("/path/to/named/pipe","w")
fifoobj.write(streamobj.read())
TroublesomeFunction("/path/to/named/pipe")

But as soon as you get to the second line the code hangs (apparently
because of the blocking behaviour of the named pipe).

Any pointers here would be much appreciated.
 
Reply With Quote
 
Iain
Guest
Posts: n/a
 
      11-10-2008
On Nov 8, 10:00*am, Iain <iain.murchl...@gmail.com> wrote:
> On Nov 7, 4:42*pm, Lawrence D'Oliveiro <l...@geek-
>
> central.gen.new_zealand> wrote:
> > In message
> > <e424a5ea-0a62-4ca1-842b-cb2cc2cea...@n33g2000pri.googlegroups.com>, Iain
> > wrote:

>
> > > Can someone give me some pointers as to how I might create some sort
> > > of blocking device file or named pipe ...

>
> > * * mkfifo /path/to/named/pipe

>
> Thanks.
>
> I did get that far myself with os.mkfifo - my problem is actually
> using it. To me it seemed like I wanted to do something like
>
> streamobj = urllib.urlopen("http://whereever.com/file")
> fifoobj = open("/path/to/named/pipe","w")
> fifoobj.write(streamobj.read())
> TroublesomeFunction("/path/to/named/pipe")
>
> But as soon as you get to the second line the code hangs (apparently
> because of the blocking behaviour of the named pipe).
>
> Any pointers here would be much appreciated.


Well I did work out *a* solution this way:

pipename = os.tmpnam()
os.mkfifo(pipename)
pid = os.fork()
if pid==0:
fifoobj = open(pipename,"w")
fifoobj.write(streamobj.read())
fifoobj.close()
os.unlink(pipename)
else:
TroublesomeFunction(pipename)

I'd have to say it doesn't strike me as the BEST solution, but it
works. In particular the use of os.tmpnam() gives a warning that its
use is a potential security vulnerability, but this is inevitable if
taking the named pipe approach (any other suggestions are most
welcome, of course). And it doesn't fail very gracefully in that if
TroublesomeFunction stops before attempting to open/read the pipe,
then the child process stays hung waiting for the other end of the
pipe to open. In an interactive python shell you also get some weird
behaviour in this situation, but I'm not entirely sure what the exact
cause of that is.
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      11-10-2008
In message
<15d2b10c-0df8-4791-874c->, Iain
wrote:

> Well I did work out *a* solution this way:
>
> pipename = os.tmpnam()
> os.mkfifo(pipename)
> pid = os.fork()
> if pid==0:
> fifoobj = open(pipename,"w")
> fifoobj.write(streamobj.read())
> fifoobj.close()
> os.unlink(pipename)
> else:
> TroublesomeFunction(pipename)


OK, so TroublesomeFunction is reading from the pipe while the child is
writing? Naturally you'd get a block if you tried to do both in the same
process.

> And it doesn't fail very gracefully in that if
> TroublesomeFunction stops before attempting to open/read the pipe,
> then the child process stays hung waiting for the other end of the
> pipe to open.


Perhaps the parent should open the pipe for reading, before calling
TroublesomeFunction. If the parent then dies, the child will get a "broken
pipe" signal, which by default should kill it.

 
Reply With Quote
 
Iain
Guest
Posts: n/a
 
      11-11-2008

> Perhaps the parent should open the pipe for reading, before calling
> TroublesomeFunction. If the parent then dies, the child will get a "broken
> pipe" signal, which by default should kill it.


Yeah, that seems to work well, I think. Thanks for the help! I also
realised the child process was continuing and returning when I didn't
really want it to. So for anyone else who stumbles across this, it
ended up being

pipename = os.tmpnam()
os.mkfifo(pipename)
pid = os.fork()
if pid==0:
fifoobj = open(pipename,"w")
fifoobj.write(streamobj.read())
fifoobj.close()
os.unlink(pipename)
os._exit(os.EX_OK)
else:
fifoopen = open(pipename,"r")
TroublesomeFunction(pipename)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"break" from a block and a proc object used in block position Wolfgang Nádasi-Donner Ruby 0 05-31-2007 06:05 PM
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Problem with enterprise application block - data block Showjumper ASP .Net 1 03-19-2005 03:48 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57