Threading Flashcards
Thread
A flow of execution. Like a separate order of instructions. However each thread takes a turn running to achieve concurrency.
GIL
Global interpreter lock- allows only one thread to hold the control of the Python interpreter at any one time
Cpu bound
Program/task spends most of its time waiting for internal events (CPU intensive) uses multiprocessing
Io bound
Program/task spends most of its time waiting for external events(user input, web scraping)
Uses multithreading
Daemon thread
A thread that runs in the background, not important for program to run, your program won’t wait got daemon threads to complete before exiting, non- daemon threads cannot normally be killed, stay alive till complete
E.g background tasks, waiting for input , garbage collection, long processes
Syntax for normal thread
Variable = threading.thread(target=-function-)
Daemon thread syntax
Variable = threading.thread(target=-function-, daemon=True)
To check if a thread is a daemon thread
Print(x.isdaemon)
Multiprocessing explained:
Running tasks in parallel on different cpu cores, bypassed GIL used for threading
What are multithreading and multiprocessing good for?
Multiprocessing = better for cpu bound tasks (heavy cpu usage)
Multithreading = better for io bound tasks (waiting around)
What does “ if __name__ == ‘__main__’:
main()
Def main():
Pass
It means that when child processes are made from the main module they will copy them but won’t execute
What are the parameters for a thread?
Variable = threading.thread(target=~~,args=(~~))
Process syntax
Variable = process(target=~~,args=(~~))
Variable.start
What function gives synchronicity?
Join() function
What part of the cpu completes processes?
The cpu cores