views:

166

answers:

8

Let's imagine a situation: I have two Python programs. The first one will write some data (str) to computer memory, and then exit. I will then start the second program which will read the in-memory data saved by the first program.

Is this possible?

+1  A: 

No.

Once the first program exits, its memory is completely gone.
You need to write to disk.

SLaks
Actually, memory based filesystems exist (at least on Linux). The app is writing to a file and that file is in memory :) But I doubt that's what Honza intended.
extraneon
+2  A: 

Store you data into "memory" using things like databases, eg dbm, sqlite, shelve, pickle, etc where your 2nd program can pick up later.

ghostdog74
+1  A: 

The first one will write some data (str) to computer memory, and then exit.

The OS will then ensure all that memory is zeroed before any other program can see it. (This is an important security measure, as the first program may have been processing your bank statement or may have had your password).

You need to write to persistent storage - probably disk. (Or you could use a ramdisk, but that's unlikely to make any difference to real-world performance).

Alternatively, why do you have 2 programs? Why not one program that does both tasks?

user9876
Actually, I don't think the OS will explicitly zero out the memory. I could be wrong, though.
SLaks
You're correct SLaks, your OS won't zero out the memory. But you have no guarantee of getting the same memory so it's useless anyway.
WoLpH
I think you're getting confused. There is a big chunk of memory that your C runtime gets from the OS. This has to be cleared (typically to zero), otherwise there would be a OS security vulnerability (suppose you got memory that held some other user's password). But when you ask the C runtime for some memory, e.g. via malloc(), that memory is NOT zeroed. You may malloc() some memory, write to it, free() it, then call malloc() again and get the same memory (which contains some leftover data from the SAME PROCESS). The Q here is whether you can see memory from an old process; you can't.
user9876
"The OS will then ensure all that memory is zeroed before any other program can see it." No, it can do whatever it pleases. There _are_ kernel patches to zero out memory on a `free()`, but this is still not going to save you from memory that was paged out or page tables that were swapped etc........
Longpoke
+4  A: 

Sort of.

python p1.py | python p2.py

If p1 writes to stdout, the data goes to memory. If p2 reads from stdin, it reads from memory.

The issue is that there's no "I will then start the second program". You must start both programs so that they share the appropriate memory (in this case, the buffer between stdout and stdin.)

S.Lott
+1  A: 

Yes.

Define a RAM file-system.

http://www.vanemery.com/Linux/Ramdisk/ramdisk.html

http://www.cyberciti.biz/faq/howto-create-linux-ram-disk-filesystem/

S.Lott
You don't need to do this, the Linux kernel already supports shared memory.
Longpoke
+1  A: 

You can also set up persistent shared memory area and have one program write to it and the other read it. However, setting up such things is somewhat dependent on the underlying O/S.

Richie
+1  A: 

Maybe the poster is talking about something like shared memory? Have a look at this: http://poshmodule.sourceforge.net/

Grissiom
A: 

What are all these nonsense answers? Of course you can share memory the way you asked, there's no technical reason you shouldn't be able to persist memory other than lack of usermode API.

In Linux you can use shared memory segments which persist even after the program that made them is gone. You can view/edit them with ipcs(1). To create them, see shmget(2) and the related syscalls.

Alternatively you can use POSIX shared memory, which is probably more portable. See shm_overview(7)

I suppose you can do it on Windows like this.

Longpoke
Forgot to mention you're going to need to make a C extension... Use SWIG for that, it will save you a lot of time compared to using the Python C API. Maybe you should check out POSH like someone else suggested, although I'm not sure if it will do what you want.
Longpoke