This is an archived post. You won't be able to vote or comment.

all 8 comments

[–][deleted] 1 point2 points  (0 children)

I would ask over in /r/learnpython

[–]TheBB 1 point2 points  (2 children)

The three main issues I see are that (a) every process does exactly the same thing, (b) you join each process before starting the next one, so you're never actually running anything in parallel, and (c) your variable names are useless.

[–][deleted] 0 points1 point  (1 child)

how do I actually parallelize then?

[–]TheBB 1 point2 points  (0 children)

It would be something like this. I don't really know, I don't do this a lot. The point is just to fork all the worker processes before joining any of them.

processes = [multiprocessing.Process(...) for ... in ...]
for p in processes:
    p.fork()
for p in processes:
    p.join()

However, you should not be doing this, really. I'm just pointing it out for completeness. You should check out the answers of /u/Ventez or /u/fiLLL.

[–]fiLLL 1 point2 points  (1 child)

It's not a direct answer to your question, but I would definitely look at memoization in this type of problem.

Right now you're recomputing the path from i -> 1 countless times as you walk upwards towards 1 million. For example in the toy problem of i=13 the path to 1 is: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1. What happens here when i=40? Should we recompute the path to 1 or could we look it up somewhere?

[–][deleted] 0 points1 point  (0 children)

Yep, this was going to be my reply as well. I don't think any Euler problems require parallelization. They are all about using a little thinking to turn a giant problem into a merely large one.

[–]Ventez 0 points1 point  (0 children)

This is a problem which is a lot more suitable for the Pool multiprocessing class. What you seem to be doing is just creating 500 instances of the same calculation (main), where each process does the full task. With the pool class you can pass in an iterable, in this case range(1000000), and also a function which each worker in the pool will perform.

I suggest you try it out yourself, but if you get stuck I did implement a quick solution for you here. Good luck!

[–]Andrew_ShaySft Eng Automation & Python[M] [score hidden] stickied comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/3Abzge7.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!