Posts: 11
Threads: 4
Joined: Feb 2022
Mar-17-2022, 09:16 PM
(This post was last modified: Mar-17-2022, 09:17 PM by Alivegamer.)
So I'm trying to learn how to use multiprocessing and It looks like I have all the code correct but still returns "Done" and doesn't print hello and hi first. import multiprocessing
def test(rt):
print(rt)
def main(ar):
print(ar)
if __name__ == '__main__':
p1 = multiprocessing.Process(target=test, args=("Hello"))
p2 = multiprocessing.Process(target=main, args=("hi"))
p1.start()
p2.start()
p1.join()
p2.join()
print("Done")
Posts: 1,032
Threads: 16
Joined: Dec 2016
This is how it works for me
import multiprocessing
def test(rt):
print(rt)
def main(ar):
print(ar)
if __name__ == '__main__':
p1 = multiprocessing.Process(target=test("Hello"))
p2 = multiprocessing.Process(target=main("hi"))
p1.start()
p2.start()
p1.join()
p2.join()
print("Done")
Posts: 4,787
Threads: 76
Joined: Jan 2018
Mar-17-2022, 09:35 PM
(This post was last modified: Mar-17-2022, 09:35 PM by Gribouillis.)
I think args should be a tuple like ("Hello",)
@ Axel_Erfurt Your code amounts to passing target=None !!!
Posts: 11
Threads: 4
Joined: Feb 2022
(Mar-17-2022, 09:31 PM)Axel_Erfurt Wrote: This is how it works for me
import multiprocessing
def test(rt):
print(rt)
def main(ar):
print(ar)
if __name__ == '__main__':
p1 = multiprocessing.Process(target=test("Hello"))
p2 = multiprocessing.Process(target=main("hi"))
p1.start()
p2.start()
p1.join()
p2.join()
print("Done") Thank you that worked
Posts: 11
Threads: 4
Joined: Feb 2022
(Mar-17-2022, 09:35 PM)Gribouillis Wrote: I think args should be a tuple like ("Hello",)
@Axel_Erfurt Your code amounts to passing target=None !!! I've tried that it still didn't work but thank you for helping
Posts: 4,787
Threads: 76
Joined: Jan 2018
Mar-17-2022, 10:04 PM
(This post was last modified: Mar-17-2022, 10:04 PM by Gribouillis.)
alivegamer Wrote:Thank you that worked It does NOT work, your print were executed in the main process. It is the same as
>>> import multiprocessing
>>> p1 = multiprocessing.Process(target=None)
>>> p1.start()
>>> p1.join()
>>> It does not complain, but it does nothing.
alivegamer Wrote:I've tried that it still didn't work but thank you for helping
This works, for me at least. I'm surprised it doesn't work for you.
import multiprocessing
def test(rt):
print(rt)
def main(ar):
print(ar)
if __name__ == '__main__':
p1 = multiprocessing.Process(target=test, args=("Hello",))
p2 = multiprocessing.Process(target=main, args=("hi",))
p1.start()
p2.start()
p1.join()
p2.join()
print("Done")
Posts: 7,319
Threads: 123
Joined: Sep 2016
Also look into concurrent.futures.
It has a simpler/better interface and work the same whatever use multiple threads or multiple processes.
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
from time import sleep
def test(rt):
sleep(10)
print(rt)
def main(ar):
sleep(3)
print(ar)
if __name__ == '__main__':
executor = ProcessPoolExecutor(max_workers=4)
executor.submit(test, 'hello')
executor.submit(main, 'hi') Set ThreadPoolExecutor and it will be threading instead.
Also set max_workers=1 then you see than need wait for hello before hi ,also no parallelism.
In this Thread a more real world example,like getting images downloaded a lot faster.
Posts: 11
Threads: 4
Joined: Feb 2022
Mar-18-2022, 12:01 AM
(This post was last modified: Mar-18-2022, 12:01 AM by Alivegamer.)
(Mar-17-2022, 10:04 PM)Gribouillis Wrote: alivegamer Wrote:Thank you that worked It does NOT work, your print were executed in the main process. It is the same as
>>> import multiprocessing
>>> p1 = multiprocessing.Process(target=None)
>>> p1.start()
>>> p1.join()
>>> It does not complain, but it does nothing.
alivegamer Wrote:I've tried that it still didn't work but thank you for helping
This works, for me at least. I'm surprised it doesn't work for you.
import multiprocessing
def test(rt):
print(rt)
def main(ar):
print(ar)
if __name__ == '__main__':
p1 = multiprocessing.Process(target=test, args=("Hello",))
p2 = multiprocessing.Process(target=main, args=("hi",))
p1.start()
p2.start()
p1.join()
p2.join()
print("Done")
Well i copied and paste your code to see if it's my errors but I still got the same problem where it will just print done. Plus I can see what you mean about it not working I've done ferther tests and your right
Posts: 7,319
Threads: 123
Joined: Sep 2016
(Mar-18-2022, 12:01 AM)Alivegamer Wrote: Well i copied and paste your code to see if it's my errors but I still got the same problem where it will just print done Remember always run from command line when test stuff like this,Editors/IDE can block process and make trouble.
G:\div_code\answer
# Look at code
λ cat multi_1.py
import multiprocessing
def test(rt):
print(rt)
def main(ar):
print(ar)
if __name__ == '__main__':
p1 = multiprocessing.Process(target=test, args=("Hello",))
p2 = multiprocessing.Process(target=main, args=("hi",))
p1.start()
p2.start()
p1.join()
p2.join()
print("Done")
# Run code
G:\div_code\answer
λ python multi_1.py
Hello
hi
Done
Posts: 11
Threads: 4
Joined: Feb 2022
(Mar-18-2022, 12:28 AM)snippsat Wrote: (Mar-18-2022, 12:01 AM)Alivegamer Wrote: Well i copied and paste your code to see if it's my errors but I still got the same problem where it will just print done Remember always run from command line when test stuff like this,Editors/IDE can block process and make trouble.
G:\div_code\answer
# Look at code
λ cat multi_1.py
import multiprocessing
def test(rt):
print(rt)
def main(ar):
print(ar)
if __name__ == '__main__':
p1 = multiprocessing.Process(target=test, args=("Hello",))
p2 = multiprocessing.Process(target=main, args=("hi",))
p1.start()
p2.start()
p1.join()
p2.join()
print("Done")
# Run code
G:\div_code\answer
λ python multi_1.py
Hello
hi
Done alright so when you said run things in cmd line and I ran the python in the cmd line it worked it's my IDLE thank you for your help!
|