Python Forum
Why doesn't this print statement work?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why doesn't this print statement work?
#1
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...
Reply
#2
I get
Output:
Sleeping for 1 second Sleeping for 1 second Done sleeping... Done sleeping... Finished in 1.0079 seconds
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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?
Reply
#4
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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?
Reply
#6
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/2364...in-ipython

Again - it is known limitation
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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
Reply
#8
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..
Reply
#9
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 331 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 984 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,875 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 926 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  Ldap3 Python print(conn.entries) doesnt work ilknurg 15 5,864 Dec-28-2022, 11:22 AM
Last Post: shad
  Why doesn't it show me anything in print? Melcu54 2 944 Oct-01-2022, 12:07 AM
Last Post: snippsat
  client.get_all_tickers() Doesn't work gerald 2 1,749 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 4,289 May-30-2022, 03:31 PM
Last Post: bowlofred
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 2,057 Dec-18-2021, 02:38 AM
Last Post: knight2000
  getting an import statement to work in my program barryjo 1 1,689 Dec-06-2021, 04:28 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020