Async Techniques and Examples in Python Transcripts
Chapter: Course conclusion and review
Lecture: Review: Threads
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
In contrast to the cooperative nature
0:02
of asyncio type of concurrency on a single thread
0:06
thread based parallelism lets the operating system
0:09
decide when our code gets to run.
0:10
We have a single process and that process
0:12
has a main thread which may kick off additional threads
0:15
spawn a couple additional pieces of work and then
0:18
wait for them to complete and keep on running.
0:20
And the important thing is it's up to the OS to figure out
0:24
when a thread runs, not up to our particular process.
0:27
Although the GIL does have something to say about
0:29
it doesn't it, so here's the mental picture for threads.
0:32
Here's the programming model, so we're going to start
0:34
with the built in threading library.
0:36
There's no external things to install here.
0:39
We're going to have some function we want to run asynchronously
0:41
but this is just normal Python code.
0:44
It doesn't do any sort of async await or stuff like
0:46
that, it's just code and instead of running on the main
0:49
thread, we're going to run somewhere else.
0:51
So given this, we'd like to create a bit of work
0:54
that's going to run it so we set up the targets
0:56
the arguments and if we want it not keep the
0:59
process alive or be canceled more easily
1:01
we can say it's a daemon thread.
1:04
And then we just start it, potentially do some
1:07
other work while it's running, or start other
1:09
threads, things like that and then we're
1:10
going to just wait for it to complete so we say work.join.
1:13
Of course we can give it a timeout.
1:16
We were just saying wait forever until it finishes.