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")
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")
I think args should be a tuple like
("Hello",)
@
Axel_Erfurt Your code amounts to passing
target=None
!!!
(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
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")
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.
(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
(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
(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!