multiprocessing - 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: multiprocessing (/thread-36680.html) Pages:
1
2
|
multiprocessing - Alivegamer - Mar-17-2022 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") RE: multiprocessing - Axel_Erfurt - Mar-17-2022 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") RE: multiprocessing - Gribouillis - Mar-17-2022 I think args should be a tuple like ("Hello",) @Axel_Erfurt Your code amounts to passing target=None !!!
RE: multiprocessing - Alivegamer - Mar-17-2022 (Mar-17-2022, 09:31 PM)Axel_Erfurt Wrote: This is how it works for meThank you that worked RE: multiprocessing - Alivegamer - Mar-17-2022 (Mar-17-2022, 09:35 PM)Gribouillis Wrote: I think args should be a tuple likeI've tried that it still didn't work but thank you for helping RE: multiprocessing - Gribouillis - Mar-17-2022 alivegamer Wrote:Thank you that workedIt 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") RE: multiprocessing - snippsat - Mar-17-2022 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. RE: multiprocessing - Alivegamer - Mar-18-2022 (Mar-17-2022, 10:04 PM)Gribouillis Wrote:alivegamer Wrote:Thank you that workedIt does NOT work, your print were executed in the main process. It is the same as 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 RE: multiprocessing - snippsat - Mar-18-2022 (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 doneRemember 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 RE: multiprocessing - Alivegamer - Mar-18-2022 (Mar-18-2022, 12:28 AM)snippsat Wrote: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!(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 doneRemember always run from command line when test stuff like this,Editors/IDE can block process and make trouble. |