0

Is it possible to copy all the memory objects in your current python program, by doing a deep copy of all memory objects. Next, start another instance of the same python program, and move those copied objects to the second running instance.

P0ppy
  • 39
  • 1
  • 7
  • You could write all the needed info to a temp file, then import that in the new instance of the program, like loading a saved file. – Davy M Feb 03 '18 at 22:30
  • I suggest you look at the module `pickle`. That allows you do save an arbitrary Python "memory object" to a file and then restore it in a working condition in another program. – BoarGules Feb 03 '18 at 22:40
  • Then, I would probably keep a complete list of objects that I need to pickle/store into a efficient file format. Reuse those objects in the second instance. I guess if i try to keep it as stateless as possible, this would definitely work. Are there any side-effects I can possibly overlook ? – P0ppy Feb 03 '18 at 23:03
  • Can you clarify exactly what you're trying to do? Creating a new instance of a program with the same memory contents of the existing one (fork) is not the same as transferring the contents of one program's memory to another program running on a different system. – UnoriginalNick Feb 03 '18 at 23:13
  • My scenario involve swapping/updating my code file on my raspberry pi without stopping the program. I was thinking of fork out another instance of the same program, and then transfer the ownership to the second instance – P0ppy Feb 03 '18 at 23:15
  • What do you mean by "transfer the ownership" of the second instance? You can't move a running process from one system to another. You also can't alter the code during execution (at least not easily or safely). Can you give a very specific example of what you're trying to do. – UnoriginalNick Feb 03 '18 at 23:22
  • what i mean is i want to shut down the first instance, once the second instance is alive and running. The ownership is effectively transferred from the first to the second. – P0ppy Feb 03 '18 at 23:26

2 Answers2

0

You mean write the info to a file, launch the script you're running, then load the info? If you mean taking the memory bytes and reallocating them to another place in your memory only to be used in the same program, I'd suggest you look at https://docs.python.org/3/library/sys.html and https://goshippo.com/blog/measure-real-size-any-python-object/

  • i don't think i want to measure the size of the object. I am thinking to run my changing the code on my raspberry pi device without shutting it down. – P0ppy Feb 03 '18 at 23:08
0

That's what forking a program does. When os.fork() is called, the child process gets a copy of the parent's memory space and both resume execution from the same point. The only difference in the two processes is the return value of the fork() call, which is 0 for the child and the PID of the child for the parent.

Here's a simple example:

import os

x = 'val'

ret = os.fork()
# At this point, both processes have the same memory contents, except for the value of `ret`
print(ret, x)
# Now change the value in the child process
if ret == 0:
    x = 'newval'
print(ret, x)

The result:

59751 val
59751 val
0 val
0 newval
UnoriginalNick
  • 327
  • 1
  • 3
  • Can I send that 'ret' val into the second process by pickling it ? – P0ppy Feb 03 '18 at 23:05
  • There are several ways to pass data between two processes. The multiprocessing.Queue class provides a simple threadsafe way to pass data between processes. – UnoriginalNick Feb 03 '18 at 23:07
  • what are some of the best practices of copying states across different processes ? – P0ppy Feb 03 '18 at 23:13
  • That's much too big of a question to answer in the comments. There are many different ways to share data between threads in a mulththreaded program, and it depends on what you're trying to do and the particular language you're trying to do it in. In general, you need to avoid situations where one thread depends on the output of another thread, or where two threads may try to modify the same resource at the same time, leading to data corruption or deadlocks. You do that both by designing your program to be amenable to multithreading, using mutual exclusion (locks, semaphores), and others. – UnoriginalNick Feb 03 '18 at 23:20