Posts

Showing posts with the label Multithreading

Introduction to network programming with Python #0003 HTTP 2020 Tutorial)

Image
  Table of Contents 1. Introduction to HTTP 2. Write a TCP server that serves as a HTTP server 3. A HTTP server demo 4. A HTTP server with multiprocessing 5. A HTTP server with multithreading 6. A HTTP server with gevent 7. A multitasking TCP server without threadings, multiprocessing, and gevent 8. HTTP Persistent connection Vs multiple connections 9. Improve our server with epoll (Linux only)

Introduction to multitasking with Python #001 multithreading (2020 tutorial)

Image
1. What is multitasking? So far, we have written so many programs with python. All the instructions inside a program run sequentially one by one. Let's look at an simple example: # multithreading_example01.py # basci program without using threading import time def sing (): for i in range ( 5 ): print ( 'Singing...' ) time . sleep( 1 ) def dance (): for i in range ( 5 ): print ( 'Dancing...' ) time . sleep( 1 ) def main (): # singing a song sing() # dancing dance() if __name__ == '__main__' : main()  After running this program, after ten seconds the program exits. But if we want to dance and sing at the same time intead of doing one after another, what we could do with the code? In python, we could use the threading module and run both sing() and dance functions at the same time. Here is how we can do it: # multithreading_example01.py