How do I call a function in Python?
Anonymous in /c/coding_help
695
report
Hi y'all. I'm using Python 3.9.2. I'm getting confused on how to start and call a function in Python. My main goal is to start a new function in a new process.<br><br>For reference, this is what I have so far:<br><br>```<br>import multiprocessing<br><br>def hello_world(s):<br> print(s)<br><br>print('Starting the function from process 1')<br>p = multiprocessing.Process(target=hello_world, args=('Hello world',))<br>p.start()<br><br>if __name__ == "__main__":<br> print("starting the function from process 2")<br> p = multiprocessing.Process(target=hello_world, args=('Hello world',))<br> p.start()<br>```<br><br>What is actually an `if __name__ == "__main__":` line in Python? What does it do? It looks like this is the line that begins the function in Python. Please let me know in the comments. Thank you for your help and your time.<br><br>EDIT: I know that this code won't work. I'm still getting confused on how to call a function in Python and start a new process that it's running in. Please let me know in the comments. Thanks for your help and your time.<br><br>EDIT2: FYI, I'm able to start a new function on a new process using the multiprocessing library. The problem I'm having is that the new function is invoked on a new process, and it also invokes the main function again and runs it again on the same process that the program is running under. For example, I tell my script to start a new function on a new process `p = multiprocessing.Process(target=hello_world, args=('Hello world',))`. That works fine, but I can also see the main function running again on the same process that the program is running under. I don't want this function to run on the same process as the program, I want it to run on a new process. I don't know how to invoke a function in a new process without it invoking the main function. Please let me know if you know how to do this. Thanks for your help and your time.
Comments (13) 25088 👁️