problem with indented in python

1

I come from the old school in c (ansi c) and I get used to the keys {}, the program runs well but I can not understand why the def worker method () has to be this indented, I mean that if I put print 'Starting% d processes ...'% WORKER_NUMBER at the same level as print "Hello, I am the process with PID% d"% PID marks me error and the "hello" prints it at the end when I thought it should be the first to print

import multiprocessing
import os


WORKER_NUMBER = 1

def worker():
    PID = os.getpid()
    print "Hello, I am the process with PID %d" % PID
print 'Starting %d processes...' % WORKER_NUMBER


if __name__ == '__main__':
    p = multiprocessing.Process(target=worker, args=())
    jobs = []
    jobs.append(p)
    p.start()
    
asked by sanlegas 03.01.2019 в 20:20
source

1 answer

1

That indentation is correct for python, but it surely does not do what you expect, since the second print() is outside the function , and therefore does not run when you call the function, but when python has finished reading the definition of that function and before passing to if _name_... .

That is, python runs in this order:

  • Execute the initial import (which consists in executing the content of those modules)
  • Run allocation WORKER_NUMBER = 1
  • Execute the def (which does not cause the execution of the function, but its definition), It keeps as a code of that function everything that is at the same level of indentation.
  • Upon detecting that the indentation has been terminated, it terminates the function code, which will only contain a print
  • Another print is found, which executes right there
  • The if is found. As the condition is true, it proceeds to interpegate what is inside.
  • Find a call to Process , so call that function. That creates a new process dedicated to executing worker (but not yet running)
  • Add the work to the list
  • Start the sub-process. At that moment worker() will be executed and the print content in that function will be output.
  • The main process ends when you reach the end of the program. The thread also ends, practically at the same time in this case.
  • I guess what you intended is that the second print would be part of the worker() . For this you must have the same indentation as the rest of the lines in that function, that is:

    def worker():
        PID = os.getpid()
        print "Hello, I am the process with PID %d" % PID
        print 'Starting %d processes...' % WORKER_NUMBER
    

    If you say that you have tried this and given an error, it is because although "visually" have the same indentation, "in fact" they do not have it, probably because the first two use four spaces while the last one uses a TAB, or vice versa.

    This mixing of TAB and spaces is one of the worst problems when editing python code. Luckily, it is resolved as soon as you start using a good editor that is aware that you are editing python code and behaves consistently. I recommend Visual Studio Code, or Vim. Otherwise, look for your editor's option to "show the invisible characters" and thus be sure that you always use spaces and never tabs.

        
    answered by 03.01.2019 / 21:31
    source