linux - Running Python interpreter inside Python interpreter: Explain behavior -


while fooling around os module in python interpreter (run inside shell on linux system), noticed possible following:

>>> os.system("python") #execute run python command in enclosing shell 

producing following output, indicating new python repl session:

python 2.7.9 (default, apr  2 2015, 15:34:55)  [gcc 4.9.2] on linux2 type "help", "copyright", "credits" or "license" more information. >>> #*blinking cursor* 

from here, possible once again make system call start new session of python, can make system call again, etc. these python environments seem independent each other in variables aren't shared amongst sessions , system calls treated equivalently.

these sessions seem run inside each other, @ least extent, rather in parallel, evidenced outcome of quit() function:

>>> os.system("python") python 2.7.9 (default, apr  2 2015, 15:34:55)  [gcc 4.9.2] on linux2 type "help", "copyright", "credits" or "license" more information. >>> quit() 0 >>> quit() 0 >>> quit() 0 >>> quit() shaked@shaked-thinkpad-x220:~/desktop$ python 

yet, quick examination (>>> os.system("ps -e")) of ongoing processes shell reveals new sh each python interpreter running:

11802 ?        00:00:00 kworker/u16:0 11803 pts/3    00:00:00 sh 11804 pts/3    00:00:00 python 11806 pts/3    00:00:00 sh 11807 pts/3    00:00:00 python 11810 pts/3    00:00:00 sh 11811 pts/3    00:00:00 python 11813 pts/3    00:00:00 sh 11814 pts/3    00:00:00 ps 

can explain (seemingly) strange behaviour in terms of underlying system processes? is, these sessions running in parallel or within each other?

apologies if question has come before, wasn't sure how others might have presented it.

as per documentation of os.system():

execute command (a string) in subshell.

os.system() forks, executes subshell passing argument subshell , waits subshell exit.

the subshell executes command , waits terminate, hence process tree is:

- python \-- sh   \-- python     \-- sh       \-- python 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -