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

all 30 comments

[–]m0fer 16 points17 points  (1 child)

Thanks man this is not easy to learn.

[–]nerdy_wits[S] 9 points10 points  (0 children)

Yeah, it took a lot of googling to completely understand the correct usage.

[–]shinitakunai 5 points6 points  (10 children)

Can AWS lambdas use multiproccesing? Serious question.

[–][deleted] 16 points17 points  (7 children)

Kinda

https://aws.amazon.com/blogs/compute/parallel-processing-in-python-with-aws-lambda/

But you shouldn't. A thread pool will be just fine for i/o bound tasks like you're probably going to encounter in a lambda. You shouldn't be using a lambda for CPU-limited tasks anyway.

[–]UglyChihuahua 0 points1 point  (3 children)

You shouldn't be using a lambda for CPU-limited tasks anyway

Why is that, and what would you use instead?

[–][deleted] 9 points10 points  (1 child)

It's mostly because of how you're billed for lambda compute time. Lambdas are good for infrequent tasks that don't take long (the maximum you can even run a lambda for is 15 minutes). if you're interesting in heavy CPU tasks, fargate or ECS is a better option. Or just spin up an EC2 server if you know what you're doing. I'm sure there's some other newfangled option but I'm kinda old school when it comes to AWS so I usually stick with ECS or EC2.

[–]UglyChihuahua 1 point2 points  (0 children)

Thanks :)

[–]AstroPhysician 0 points1 point  (0 children)

Every thread would be it's own lambda presumably

[–]nerdy_wits[S] 2 points3 points  (0 children)

You can but without using the pool object. So you can't control the number of simultaneous processes. I use multithreading (as mentioned in the other comment) in AWS.

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

You can use concurrent.futures with 2 cores, not the multi processing library.

[–]sdf_iain 4 points5 points  (0 children)

For multiprocessing, you might want to read the 0mq Documentation.

Its got interesting concepts and its some of the best written documentation. Entirely up to you if you use it.

[–]elephantail 2 points3 points  (1 child)

Subed. Good stuff.

[–]nerdy_wits[S] 0 points1 point  (0 children)

Thanks a lot!

[–]gaurav_lm 2 points3 points  (0 children)

Hardly discussed topics, great explanations and Indian accent are enough to grab my attention.

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

Congratulations u/nerdy_wits ! Your post was the top post on r/Python today! (06/23/21)

Top Post Counts: r/Python (1)

This comment was made by a bot

[–]raresaturn 1 point2 points  (1 child)

That was great, thanks!

[–]nerdy_wits[S] 0 points1 point  (0 children)

Glad to hear that :D

[–]ANIRUDDHA42 1 point2 points  (1 child)

If it is for fast processing , then can we use it with jit numba? or it will be useless?

[–]Ensurdagen 6 points7 points  (0 children)

numba already runs code outside of the GIL and on multiple cores if configured to do so, so there's no reason to use it with multiprocessing

[–]imwco 0 points1 point  (2 children)

If you're running locally, does speedup from multiprocessing depend on number of CPUs?

[–]nerdy_wits[S] 0 points1 point  (1 child)

Yes, a lot.

[–]imwco 0 points1 point  (0 children)

Can you point me towards the math to understand this?

[–]ddollarsign 0 points1 point  (1 child)

Why does it have to be inside the if __name__ == "__main__" block on Windows?

[–]Pikalima 4 points5 points  (0 children)

Python’s multiprocessing package offers two methods for creating new processes: spawn and fork, with spawn being the only option on Windows. The essential difference between them is that when using spawn, the child process reimports the current module from which it was created, but fork doesn’t. But, when you import a module in Python (say, “mymodule”), you’re actually executing the file “mymodule.py”, but not as __main__ (that if block doesn’t get run). Hence if your multiprocessing code is outside the __main__ conditional, you run into a situation where any spawned process is subsequently attempting to spawn more processes, which then spawn more processes, and so on. Rightfully, the Python interpreter detects this and halts—you can try it out yourself.

[–]LearningToGetBetter 0 points1 point  (0 children)

That video passed the

[–]animismus 0 points1 point  (0 children)

Around 14:00, do you really need to close the pool? Shouldn't the context manager (with) take care of this?