473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

creating a block file for file-like object

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
Nov 7 '08 #1
5 1905
In message
<e4**********************************@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
Nov 7 '08 #2
On Nov 7, 4:42*pm, Lawrence D'Oliveiro <l...@geek-
central.gen.new_zealandwrote:
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.
Nov 8 '08 #3
On Nov 8, 10:00*am, Iain <iain.murchl...@gmail.comwrote:
On Nov 7, 4:42*pm, Lawrence D'Oliveiro <l...@geek-

central.gen.new_zealandwrote:
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.
Nov 10 '08 #4
In message
<15**********************************@v22g2000pro. googlegroups.com>, 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.

Nov 10 '08 #5
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)
Nov 11 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
7
by: Jan Danielsson | last post by:
Hello all, Is there any way to create a file with a specified size?
20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
6
by: Peter Afonin | last post by:
Hello: I'm trying to convert the connection class that I created in VB.Net for ASP.Net application into C#. I'm doing something incorrectly, because I'm getting the following error when trying...
0
by: zhimin | last post by:
Hi, I'm writing a program to send large file(100m) through dotnet using TCPListener & TCPClient, I'm sending the file with a ask and response loop: 1. Client send a flag 1 to server indicate it...
7
by: Selden McCabe | last post by:
I'm using the following code to write some text to a file: objWriter = New StreamWriter(FullPath, True, System.Text.Encoding.ASCII) For nRow = 1 To sData.Length - 1...
3
by: Sean C. | last post by:
Hey All, I'm having a little problem here. I have a project that I'm working on that involves a MySQL server database backend. I'm having no problem creating the database on the fly if it...
4
by: Fritjolf | last post by:
Hi. I've got a strange problem... I've made a simple program to test encryption/decryption. I use Rijndael encryption and here are the most important properties. RijndaelManaged cipher =...
0
by: Jean-Paul Calderone | last post by:
On Tue, 29 Jul 2008 07:26:38 -0700 (PDT), "jiri.zahradil@gmail.com" <jiri.zahradil@gmail.comwrote: Python doesn't have dynamic scoping. ... for i in range(n): ... callable()...
1
by: stepthom | last post by:
I am attempting to break my templates down from 1 main xsl file into smaller files. Then I want to call them from a main xsl file. I started to look. I have found xsl:include and xsl:import. Also,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.