简体   繁体   中英

Threading using less RAM than Popen, why?

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...

The script with threading: 穿线

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM