Python Forum
Why doesn't this print statement work? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why doesn't this print statement work? (/thread-33029.html)

Pages: 1 2


Why doesn't this print statement work? - stylingpat - Mar-23-2021

Hey guys, I'm learning about multiprocessing. If I run this code, I don't get the print "Sleeping for 1 second". But if I run the line do_something(), I'll get both prints. Any reason why? It doesnt do it with Anaconda or IDLE.

import multiprocessing
import time

start = time.perf_counter()

def do_something():
    print('Sleeping for 1 second')
    time.sleep(1)
    print('Done sleeping...')
    
if __name__ == '__main__':
    
    p1 = multiprocessing.Process(target=do_something)
    p2 = multiprocessing.Process(target=do_something)
    
    p1.start()
    p2.start()
    
    p1.join()
    p2.join

    finish = time.perf_counter()
    print(f'Finished in {round(finish-start,4)} seconds')
Here's my outputs

Output:
runfile('C:/Users/zenfi/OneDrive/Desktop/multiprocessing/conda_testing.py', wdir='C:/Users/zenfi/OneDrive/Desktop/multiprocessing') Finished in 1.079 seconds do_something() Sleeping for 1 second Done sleeping...



RE: Why doesn't this print statement work? - buran - Mar-23-2021

I get
Output:
Sleeping for 1 second Sleeping for 1 second Done sleeping... Done sleeping... Finished in 1.0079 seconds



RE: Why doesn't this print statement work? - stylingpat - Mar-23-2021

Yeah thats what the guy gets on the video? Why wouldnt I get that?

So I just ran it in the terminal and I get the same thing as you now.. but running it in idle or anaconda I dont get it? What would be different?


RE: Why doesn't this print statement work? - buran - Mar-23-2021

for IDLE check https://stackoverflow.com/q/35293178/4046632

I don't use IDLE or anaconda, but there are multiple issues/posts about it if you google it.


RE: Why doesn't this print statement work? - stylingpat - Mar-23-2021

This is my v3.9 IDLE

Output:
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> === RESTART: C:\Users\zenfi\OneDrive\Desktop\multiprocessing\conda_testing.py == Finished in 1.0731 seconds >>>
This is my v3.8 Virtual Enviroment using IDLE

Output:
Python 3.8.8 (default, Feb 24 2021, 15:54:32) [MSC v.1928 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> === RESTART: C:\Users\zenfi\OneDrive\Desktop\multiprocessing\conda_testing.py == Finished in 1.0775 seconds >>>
This is my v3.8 Virtual Enviroment using Anaconda

Output:
Python 3.8.8 (default, Feb 24 2021, 15:54:32) [MSC v.1928 64 bit (AMD64)] Type "copyright", "credits" or "license" for more information. IPython 7.18.1 -- An enhanced Interactive Python. runfile('C:/Users/zenfi/OneDrive/Desktop/multiprocessing/conda_testing.py', wdir='C:/Users/zenfi/OneDrive/Desktop/multiprocessing') Finished in 1.0761 seconds In [2]:
This is what I get in my Anaconda Prompt 3.9

Output:
(base) C:\Users\zenfi\OneDrive\Desktop\multiprocessing>conda_testing.py Sleeping for 1 second Sleeping for 1 second Done sleeping... Done sleeping... Finished in 1.0843 seconds
And Anaconda Prompt 3.8

Output:
(3candles) C:\Users\zenfi\OneDrive\Desktop\multiprocessing>conda_testing.py Sleeping for 1 second Sleeping for 1 second Done sleeping... Done sleeping... Finished in 1.0913 seconds
Why would the two prompts be any different from the applications?


RE: Why doesn't this print statement work? - buran - Mar-23-2021

Did you check the link in my previous post? It explains why it does not work when start IDLE from icon (vs. starting from cmd).

Also https://bugs.python.org/issue11820

For IPython I think this is helpful https://stackoverflow.com/questions/23641475/multiprocessing-working-in-python-but-not-in-ipython

Again - it is known limitation


RE: Why doesn't this print statement work? - stylingpat - Mar-23-2021

Awesome thank you so much, I'll go through the pages. Sorry alot of the stuff on stackoverflow just goes way over my head.. Cool


RE: Why doesn't this print statement work? - stylingpat - Mar-23-2021

Like this stuff.. I just cannot comprehend

class classname:
    def  createname(self, name):
        self.name = name;
    def displayname(self):
        return self.name;
    def saying(self):
        print("Hello %s" % self.name);

first = classname;
second = classname;

first.createname("Bobby");
I undersstand the idea, but all the "names" or "labels" of everything is way to similar.. I need this example, with like a pizza and different toppings, cheese, mushrooms, onions... seperate looking words.

I feel like everytime I goto stack overflow, its just that..


RE: Why doesn't this print statement work? - buran - Mar-23-2021

First of all this is very poor example of python code (i.e. unpythonic) and of implementation of custom class in python in particular. But I would suggest you get familiar with fundamentals before you go into more complex things like class


RE: Why doesn't this print statement work? - stylingpat - Mar-23-2021

Its kinda funny thinking about this now, how I can't look at that yet and understand it. Big Grin That block of code is a blur to my eyes and brain. Its not because its intellectually out of reach, that my brain cant process the connections. Its the way its presented. Everything in that block of code is too similar.

Its made me realize where my success in life has come from. I'm not super smart, like raw mental mathematical processing smart.. I just have an infinite amount of patience when it comes to learning something. Its pretty cool to see how far that has gotten me!

Maybe I'll sit down tonight, proper sit down and tackle that block of code LOL Think