Knowledge Base
main
main
  • Hello , There !!!
  • Blogs , Thoughts , Etc
    • Blogs
      • What happens when you type a URL into your browser?
      • HTTP Keep Alive Vs TCP keep Alive
      • How Browsers Work ?
      • Change Data Capture @ MySQL
    • Batch Vs Stream Processing
    • Book Summaries
      • 12 Rules for Life
    • Random Thoughts
      • Being On The Right Side Of The “Zero”
      • I ̶F̶̶a̶̶i̶̶l̶̶e̶̶d̶ Learnt Something New.
  • NODE JS
    • Parsing HTML DOM
    • Make HTTP Request
    • Read SQLite File
    • Remove Directory Recursively
  • JavaScript
    • Event Loop In JavaScript
  • Numbers
    • Untitled
  • Random
    • Google File System
    • Concurrency vs Parallelism
    • Spark
    • Apache Kafka[WIP]
  • Python
    • Python Memory Model
    • Multithreading With LightWeight Threads
    • Logger
  • JAVA
    • Java Memory Model
  • SQL
    • Untitled
    • Page 1
  • Machine Learning
    • Page 2
  • Productivity
    • Git
    • ssh
  • Hacks
    • Good Youtube Channels
Powered by GitBook
On this page

Was this helpful?

  1. Python

Multithreading With LightWeight Threads

import json
import urllib.request
from urllib.parse import urlparse
from threading import Thread
import http.client, sys
from queue import Queue
from abc import abstractmethod, ABC

concurrent = 50
tasks = []

class Task(ABC):

    @abstractmethod
    def Init(self):
        raise NotImplementedError

    @abstractmethod
    def CleanUp(self):
        raise NotImplementedError

    @abstractmethod
    def CheckStatus(self):
        raise NotImplementedError

    @abstractmethod
    def Run(self):
        raise NotImplementedError
                

def doWork():
    while True:
        task = q.get()
        task.Init()
        task.Run()
        task.CleanUp()
        q.task_done()

#starting daemon threads 
q = Queue(concurrent * 2)
for i in range(concurrent):
    t = Thread(target=doWork)
    t.daemon = True
    t.start()
    
    
try:
    for task in tasks:
        q.put(task)
    q.join()
    
except KeyboardInterrupt:
    sys.exit(1)

Queue - A synchronized queue class

PreviousPython Memory ModelNextLogger

Last updated 3 years ago

Was this helpful?