r/learnpython • • Feb 20 '21

What are you having difficulty learning or are currently struggling to learn?

I'm curious what the community here has difficulty with when it comes to learning Python. Partly because I want to see if the more experienced community here (or myself) can help point you into the right direction. . . but also because I'm trying to come up with video tutorial ideas for future content and would love to hear from Python learners what would be most useful to dedicate our time on.

I'm looking forward to hearing what you guys have to say!

417 Upvotes

324 comments sorted by

View all comments

3

u/lucadenhez Feb 21 '21

Thanks for helping us with some concepts we are struggling with! I would say one thing that’s pretty hard for me is grasping the concept of asynchronous methods and multi-threading. Like one question I have is does python only use one thread normally until I ask it to use multiple threads? Is the only advantage performance? And why use async over sync? All general questions like that.

1

u/MaraudingAvenger Feb 21 '21

We're definitely going to cover concurrency in python in one of future streams, it's part of the class we teach, so stay tuned there -- the biggest thing is that sometimes while your code is waiting for something external to happen, it's just sitting there idle.

When your computer sends a request to an external API for data -- let's say you're requesting a bunch of coordinate lookups from the Google maps API -- the majority of the time that it's sending and receiving responses it's just sitting there waiting for something to happen.

If you're doing things linearly, you have a list of a couple hundred coordinates. Python grabs one, formats a request and sends it out, then it just sits and waits until Google sends the response back. That could take a couple of milliseconds to do, those are like computer years. Your processor has been just sitting there for millions of nanoseconds twiddling its thumbs waiting for something to happen.

Concurrency (async def functions, threading, and multiprocessing) all say, "how about we move onto the next thing while we wait for the first thing to finish?"

The differences between the three of them:

Multiprocessing

I am a mathematician doing 8 math multivariable calculus problems. It would be awesome if I had 7 more mathematicians (processes) to do these with me. I could try to do all 8 of them at once by myself (threading), but that would actually make me solve them slower.

Multiprocessing spawns a new process that uses a different CPU core. Basically copies the running python program to that/those CPU cores, so it's a bit heavyweight. Use this for big number crunching operations. Like the one mathematician trying to solve all 8 problems at once, if you try to use threading (instead of multiprocessing) for CPU intensive stuff, sometimes it'll actually be slower than doing it linearly.

Threading

I am cooking dinner. I have to season and boil potatoes, season and bake a turkey, toast and butter some bread, and season and sautee some onions. It would be waaaaaay faster if I could do the prep and just start one thing, then move onto the next thing while I wait for the first thing to cook.

Threading (not related to "threads" when referring to CPU architecture) are semi-independent tasks that all run on the same process. Think of it like multitasking. You can do decently complicated things at once, but it's all you (the main process) doing them. Threads take turns doing things. Basically they wait for some down time while the CPU isn't doing something else, and then run some quick numbers, then give up their CPU time to another thread. This is all managed by the python interpreter, and you may hear people bemoaning the Global Interpreter Lock (GIL). This is the thing that makes it so that only one thread can be doing a thing at one time -- and it's fine. People bashing the GIL are usually also condemning you for blaspheming the Javachrist, our lord and C-avior. Flex your 10 line flask server at them. They won't accept it, but they will weep when nobody's looking.

Async/await + asyncio

I am feeding the Twilight novels into a paper shredder. Unfortunately they are 400-ish pages and the paper shredder is ponderously slow. I have other stuff to do, and I want to decide when to go back to shredding Twilight books. It's not a difficult task, but it takes awhile.

async def functions are great when some task is I/O bound (held up by the speed that something's going out or coming in). They're also good for things like web requests, so you could use them there as well. The key with async is that it's not another thread; it's something called cooperative multitasking, or a coroutine. It's single thread, single process. An async function can stop halfway through and wait for a more opportune time to pick its task up and keep going.

For all three:

  • CPU heavy? - multiprocessing

  • I/O bound? - asyncio

  • everything else: threading

Hope that helps!