473,804 Members | 2,673 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1920
In message
<e4************ *************** *******@n33g200 0pri.googlegrou ps.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...@n3 3g2000pri.googl egroups.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(s treamobj.read() )
TroublesomeFunc tion("/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...@n3 3g2000pri.googl egroups.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(s treamobj.read() )
TroublesomeFunc tion("/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(pipen ame)
pid = os.fork()
if pid==0:
fifoobj = open(pipename," w")
fifoobj.write(s treamobj.read() )
fifoobj.close()
os.unlink(pipen ame)
else:
TroublesomeFunc tion(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
TroublesomeFunc tion 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************ *************** *******@v22g200 0pro.googlegrou ps.com>, Iain
wrote:
Well I did work out *a* solution this way:

pipename = os.tmpnam()
os.mkfifo(pipen ame)
pid = os.fork()
if pid==0:
fifoobj = open(pipename," w")
fifoobj.write(s treamobj.read() )
fifoobj.close()
os.unlink(pipen ame)
else:
TroublesomeFunc tion(pipename)
OK, so TroublesomeFunc tion 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
TroublesomeFunc tion 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
TroublesomeFunc tion. 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
TroublesomeFunc tion. 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(pipen ame)
pid = os.fork()
if pid==0:
fifoobj = open(pipename," w")
fifoobj.write(s treamobj.read() )
fifoobj.close()
os.unlink(pipen ame)
os._exit(os.EX_ OK)
else:
fifoopen = open(pipename," r")
TroublesomeFunc tion(pipename)
Nov 11 '08 #6

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

Similar topics

699
34275
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
7
7517
by: Jan Danielsson | last post by:
Hello all, Is there any way to create a file with a specified size?
20
3082
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: --cut here-- typedef struct pkg_ { short int info; char* data;
6
1592
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 to compile the project: "D:\C#_Projects\connect_class\connect_class\clConnect.cs(18): 'connect_class.clConnect.CreateDataset(string)': not all code paths return a value"
0
913
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 has data send to server. 2. Client send the buffer block size. 3. Client send the actual buffer to the server. 4. Server send a flag 1 to client indicating that the buffer has been successfully receeived. 5. The next loop until all data of the...
7
3956
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 objWriter.WriteLine(sData(nRow)) Next nRow objWriter.Flush() objWriter.Close() When I run this in the IDE, it works. When I compile it and run the EXE, it
3
4503
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 doesn't already exist and using it once it's created. My problem comes into play when I run the program for the first time. I'm wanting to have some kind of flag that lets me know that the database has not yet been created, so that I can call my...
4
5705
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 = new RijndaelManaged(); cipher.KeySize = 256; cipher.BlockSize = 256;
0
856
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() ... ... for j in range(i): ... print j, ... print
1
1591
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, not sure to use apply-templates or call-templates. Trying to find good examples on both w/ the main file and source file, but have it a road block. Does anyone have ideas on this or good xsl sites that I can refer too. ~thanks!
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9140
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.