How does the program inside the terminal run?

Source: Internet
Author: User

For example, in a Mac environment under a certain path to run celery task, celery-a msg_task worker for this paragraph of the understanding of the sentence is actually used in the back of these parameters to run celery executable file; How exactly is the run up, divided into the following four steps.

one: Environment variables for Mac
Mac Bash is generally used as an environment variable for the default Shell,mac system, loaded in the following order:
Ⅰ. (System level, system boot is loaded)
/etc/profile/etc/paths
Ⅱ. (Current user-level environment variables, because ~ The folder itself is the home directory of the login, depending on the shell, the loading of the configuration file may be different, for example, if the use of zsh, then the first file on this side will be ~/.ZSHRC; then if the ~/.bash_profile file exists, The subsequent file will be ignored, and if it does not, it will read the following file
~/.bash_profile ~/.bash_login ~/.profile
Ⅲ. (Loaded when the bash shell is open)
~/.bashrc< br> like my computer above see the contents of the/etc/path file is
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
and then in the ~ The content of/.zprofile is path= "/library/frameworks/python.framework/versions/3.4/bin:${path}"
the last content in ~/.ZSHRC is export Path=/usr/local/sbin: $HOME/bin:/usr/local/bin: $PATH
So finally we print the environment variable PATH in the terminal, using the echo $PATH to get the information is/usr/local/sbin :/users/sunmenghua/bin:/usr/local/bin:/library/frameworks/python.framework/versions/3.4/bin:/usr/local/bin:/ Usr/bin:/bin:/usr/sbin:/sbin, this corresponds to the

Two: Find the executable file
Under/usr/local/bin "Path 1" and/library/frameworks/python.framework/versions/3.4/bin "Path 2" path, we all found the celery
However, when executing the celery-a msg_task worker, it was found that execution had an error, saying "Path 1" below the celery is a wrong compiler (bad interpreter), and/usr/ocal/opt/python3/bin/ python3.5 does not exist, so find the corresponding executable file in "Path 2"

Three: Parse executable file

  Open the file, can be located to the portal is in the celery module __main__.py inside the polygon function, see the content is: 

1 ImportRe2 ImportSYS3  fromCelery.__main__ ImportMain4 if __name__=='__main__':5Sys.argv[0] = Re.sub (r'(-script\.pyw|\.exe)? $',"', Sys.argv[0])#regular match and replace6Sys.exit (Main ())#run the main function from the __main__.py inside the celery module when exiting

off-topic: found a more useful module, is the SYS module. Before I wrote the script, I used the Tornado.options module.    The form of use is:  

1  fromTornado.optionsImportOptions, define#Introducing Modules2Define"Action", default="Userinc", help="Update static module")#define the parameters passed in3 if __name__=="__main__":4Options.parse_command_line ()#Parse all parameters passed in the command line, default is SYS.ARGV, then discard the first parameter sys.argv[0], because it is the program name5Action=options.actionthen call form is Python3 test.py--action=test, after tracking tornado source code, know that this module is actually in Sys.args a layer of encapsulation
Four: Find the Python3 module  
Via import celery, then help (celery),
Ⅰ. Find the source code of the module is located in/library/frameworks/python.framework/versions/3.4/lib/python3.4/site-packages/celery/;
Ⅱ. Use sublime to open the corresponding folder, first find __main__.py, in accordance with the import module to find the main function inside the celery/bin/celery.py
Ⅲ. Find the execution method is the Execute_from_commandline in class Celerycommand;
Ⅳ. Continue to trace upward, execute the method in Celery.bin.base inside the execute_from_commandline;
Ⅴ. Print debug, real read parameters are in Setup_app_from_command (argv)
Ⅵ. Print debugging, go down to Self.find_app (app);
Ⅶ. Called by the From Celery.app.utils import Find_app method passed to his two parameters, one app is passed down the parameters, the other is the Symbol_by_name, this parameter is a function, make very complex, simply too complex, A large circle around.
## #这里尝试描述一下:##A. Find_app (app, symbol_by_name=self.symbol_by_name) uses the functions inside the current class;##B. From celery.utils Import Imports##defSymbol_by_name (self, name, imp=imports.import_from_cwd): # #returnImports.symbol_by_name (Name, imp=imp)##C0. But in celery/utils.py there's a from celery.utils.imports import Import_from_cwd,symbol_by_name##D0. Continue to celery/utils/imports.py inside and find: from kombu.utils.imports import Symbol_by_name##E0. Find kombu/utils/imports.py, indeed the last import is here, but the logic here is if there is no conduction into the function, then use Importlib.import_module to do the import function, Otherwise, the import function is passed when the call is made; Well, when we use, actually in step b there is an import function, then we continue to chase B to see where this import function is passed in;##C1: Just like C0 .##D1: Like this form of B, there is no parameter, so the function obtained here is also importlib.import_module

The above tracking does not continue to describe the words are difficult to express.
But in the course of tracing, there are a few understanding.
1. The function does not execute if it is not called.
Write a simplified call.

 def   T1 ():  print  ( " t1  "  )  def  Main (c= t1): C ()  print  (  " main   " )  if  __name __  = =  " __main__  "  : Main ()  
defprint('t1')def Main (c=Print ('main')if__name__'__ main__':    main ()


The first print is T1 and main, and the second one is the main
2. After talking to others, I thought this code should be considered a bad code, but was told that this difficult to understand the code is generated by object-oriented programming and product iteration, when invoking a new module, do not need to know his internal implementation principle, only need his parameters and return, Then if you want to expand him, just expand the interface directly.

But I still don't quite understand, think about it later.

How does the program inside the terminal run?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.