How to implement multithreading

Because forking isn't fully cross-platform compatible, there are two primary workers used in parallel Python programming: threads and processes. Threads are typically the "go-to" parallel tool for many programmers. Simply put, threads are separate workers that function simultaneously to complete the larger job. One job can have multiple threads.

A good example is a web browser: while the browser itself is a single process when viewed in Windows Task Manager or using the ps command in Linux, the browser can spawn many threads to accomplish tasks, such as going to a URL, rendering HTML, processing JavaScript, and so on. All those threads are working together to accomplish the mission of the browser process.

Threads are sometimes called lightweight processes because they run in parallel like *nix forked processes, but they are actually generated by a single parent process. Threads are frequently used in graphical interfaces to wait for, and respond to, user interaction. They are also prime candidates for programs that can be designed into multiple, independent tasks; this makes them ideal for networking, where I/O operations are the bottleneck, rather than the CPU.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset