0

I'm trying to integrate multiprocessing into a project but I can't get it working. This is what I've got:

import time import winsound from multiprocessing import Process winsound.MessageBeep() def pr1(): while 1: winsound.MessageBeep() time.sleep(0.5) if __name__ == '__main__': p = Process(target=pr1, args=()) p.start() p.join() while 1: print('hey') 

but if I run it i hear only one beep and i want it to repeat. How do I get this done?

oke plan b, I've got this now and I only get correct:

import time import winsound from multiprocessing import Process def pr1(): while 1: winsound.MessageBeep() print('its working') time.sleep(0.5) if __name__ == '__main__': print('correct') p = Process(target=pr1, args=()) p.start() p.join() while 1: print('hey') 

So there is something wrong with with the creating of the process. Any ideas?

8
  • if __name__ == '__main__': works? Commented Aug 7, 2013 at 12:13
  • I'm not on Windows, so cannot check it, but it looks like it should work. What happens if you add a print statement before or after the call to winsound.MessageBeep()? Can you see it printing something? Commented Aug 7, 2013 at 12:13
  • that's odd, I can't even get the print hello example on the Python doc page to work. I've never used multiprocessing but I have used threading. Not sure if that would meet your needs, but it works rather well. tutorialspoint.com/python/python_multithreading.htm Commented Aug 7, 2013 at 12:20
  • I changed my code to this and now the only thing on my screen is correct. import time import winsound from multiprocessing import Process def pr1(): while 1: winsound.MessageBeep() print('its working') time.sleep(0.5) if name == 'main': print('correct') p = Process(target=pr1, args=()) p.start() p.join() while 1: print('hey') Commented Aug 7, 2013 at 12:23
  • Update the code in your question. Nobody can read Python in comments. Commented Aug 7, 2013 at 12:26

2 Answers 2

2

Indent the final

while 1: print('hey') 

to make it part of the if-block

When starting the child process under Windows the module contents are first executed before the callable given as target is run. Because the module never finishes execution, this doesn't happen.

The second snippet as a whole then becomes:

import time import winsound from multiprocessing import Process def pr1(): while 1: winsound.MessageBeep() print('its working') time.sleep(0.5) if __name__ == '__main__': print('correct') p = Process(target=pr1, args=()) p.start() p.join() while 1: print('hey') 
Sign up to request clarification or add additional context in comments.

6 Comments

I don't know what you mean. If indent the while statement, northing happens or do I have to move it?
@CvR_XX If you indent it, it is only executed if the if-statement is true (which only happens in parent process). I tried it with your second code snippet and it works for me.
I could be a little sleepy but I still don't understand can you please post the second snippet with you edits.
the strange thing is that, even with this code, I only see "correct" and not "hey" and "it's working"
good job windows, It's suddenly start working causing a lot of noise and a slow pc because of hundreds of python processes
|
0

To show the "its working" message you need to flush the buffer, because usually in processes the output is normally buffered.

import time import winsound from multiprocessing import Process import sys def pr1(): while 1: winsound.MessageBeep() print('its working') time.sleep(0.5) sys.stdout.flush() if __name__ == '__main__': print('correct') p = Process(target=pr1, args=()) p.start() p.join() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.