-2

If I have two scripts, one has a list and is currently running and the other one needs to access that list while the first one is running, how would I do that?

Example

Script 1

import random
example_list = []
while True:
    example_list.append(random.randint(0,9))

Script 2

x = example_list[i]

I can not change Script 1.
How would I access the list created in Script 1 from Script 2?

P.S. This is just an example so it's purpose doesn't matter.

Markus
  • 331
  • 2
  • 4
  • 13
  • `list = list + random.randint(0,9)` is a `TypeError`. Also `list` shadows the built-in type `list`: `list() == []`. – ForceBru Oct 29 '17 at 17:36
  • You are also appending a list in a `while True` which is kinda silly. – scharette Oct 29 '17 at 17:38
  • I was thinking they might be a way to use pickle and then get the data from there but I don't know if I could do that without editing the first script. – Markus Oct 29 '17 at 18:06

3 Answers3

0

I think what you are looking for is interprocess communication (IPC) where 1st script is populating a variable and 2nd script wants to use it. There could be several mechanisms for IPC - message queues, shared files, etc. If your requirement is as simple as to capture the list contents (and list contains strings, integers alone) such that other script can use it, you may dump it into a known file as python code or JSON and read it from the 2nd script. You may pickle the data and capture the pickled data in a file and unpickle in the 2nd script - provide the data in list is picklable.

Sharad
  • 9,282
  • 3
  • 19
  • 36
  • I'm attempting to access the data from the first script while it's running without changing the code of the first script. I was trying to figure out if they're was a way to access that data from a second script without changing the first. – Markus Oct 29 '17 at 17:59
  • Take a look at pyrasite - http://pyrasite.com/. This allows you to connect to a runinng python shell, read data from it or inject code. – Sharad Oct 30 '17 at 04:05
0

If you can use multiprocessing (one process starts the other), then you can share the list using multiprocessing capabilities. Have a look at Multiprocesssing

John Anderson
  • 35,991
  • 4
  • 13
  • 36
  • This wouldn't work because I am trying to access the data from the first script as it's running so I can't edit it as that would require me to stop the script. – Markus Oct 29 '17 at 17:56
  • You can use gdb to access the running process and dump the memory heap. I have seen instances of interpreting the heap to find string variables, but it takes more knowledge than I have to do it. Check https://blog.holbertonschool.com/hack-the-virtual-memory-python-bytes/ – John Anderson Oct 29 '17 at 21:53
0

No! You can not do it. At least in python.

There is no way you can access a variable in another running process.

Elis Byberi
  • 1,422
  • 1
  • 11
  • 20
  • Do you know if I can save the execution state of script 1 with pickle, and access the list from there. – Markus Oct 29 '17 at 18:09
  • No! You can not save execution state with pickle or any other library. – Elis Byberi Oct 29 '17 at 18:10
  • @ElisByberi I think pyrasite (www.pyrasite.com) allows you to connect to a runinng python shell (based on pid), read data from it or inject code. – Sharad Oct 30 '17 at 06:13
  • @Sharad Theoretically it is not possible to inject, edit, or whatever change you may want to do in a running python script. We say "running python script" but it is not a script anymore. It has been translated in C language and from C to machine binary code. You can not do it without reloading script execution which is not what we want. – Elis Byberi Oct 30 '17 at 07:21