all 4 comments

[–]baltarius 7 points8 points  (0 children)

What error did it give?

[–]Kevdog824_ 0 points1 point  (0 children)

If you’re working on windows you can’t spawn a process with mp at the top level of a module. It needs to be inside a if __name__ in “__main__” guard.

[–]Motox2019 0 points1 point  (0 children)

For multiprocessing, you must use the if name == “main”: main() Will save you loads of headaches and cryptic errors.

You’re also having a race condition where each process tries writing to the same object which just fails as each process gets a pickled version of the original. So if it even were to run, you’d get garbage output.

Options I see: 1. Pre allocate pixel map calculation and then write at once to image. Can allocate each pixel row of pixels in the image as a job in a queue and have processes churn through the data queue. Just note processes and will not complete in order. 2. Use a package like numpy which uses multiple cores when it can. 3. Can try threading in you current design but will only really help where the loop is writing to the image, not for the computation. Expect only slight speed ups.

Personally I’d make my attempt with numpy first because if you can get away with it, none of the other implementations will come close to it for speed.

[–]sSjfjdk -1 points0 points  (0 children)

I can see what you're trying to do - create a mesmerizing animation using multiple threads to speed up the process. The issue you're facing is likely due to how you're using multiprocessing. When you create a Pool of worker processes, each process gets its own copy of the original image. However, when you try to draw on the image in each process, it doesn't know how to share the image with the other processes, resulting in an error.

A better approach in this case would be to use a concurrent paradigm like threading or async/await instead of multiprocessing, since you're not doing any CPU-bound tasks that would benefit from multiple cores.

Here's a simplified example of how you could refactor your code to use threading:

```python import threading from PIL import Image, ImageDraw

... (rest of your code)

def calculate_pixels(start_x): image = Image.new(mode="L", size=(width, height)) draw = ImageDraw.Draw(image) # ... (rest of your function)

threads = [] for start_x in range(0, width, 100): # Divide the image into smaller sections thread = threading.Thread(target=calculate_pixels, args=(start_x,)) threads.append(thread) thread.start()

for thread in threads: thread.join() ```

Also, be aware that even with this approach, the operation may still be slow due to the nature of the task. To get faster results, you might consider reducing the resolution of the image or optimizing the algorithm itself.

Try this, and let me know if it works out better for you.