I have a question, So I am testing RAM usage of my script and it is as easy as:
a script that is a start up, that script opens up 4 python script in a while loop and loops forever.
I tested 2 things. One with just calling Popen for each script and one using threading and I found out that there is a huge difference between each other...
And a script that uses Popen to open up the scripts:
Since they both do the exact same thing, why does the Python threading uses so much less RAM than the other that opens up the script? What are the advantage vs. disadvantage of using the threading vs. Popen?
test2:
import time
def testing():
while True:
test = "Helllo world"
print(test)
time.sleep(1)
if __name__ == '__main__':
testing()
threading.py:
import threading
from test2 import testing
threading.Thread(target=testing()).start()
threading.Thread(target=testing()).start()
threading.Thread(target=testing()).start()
Popen:
from subprocess import Popen
for _ in range(4):
Popen(f"py test2.py", shell=True).communicate()
The popen()
version creates 3 whole new processes, each of which runs its own, distinct Python interpreter.
The threading version run 3 threads within the current process, sharing its memory space and the single, original Python interpreter.