Posts

Showing posts from September, 2022

Python Multithreading--Part3 when multiple thread calling one method of code simulteniously then Lock(), acquire() and release() method will do synchronisation....

 # -*- coding: utf-8 -*- """ Created on Fri Sep 30 14:01:03 2022 @author: Mahesh.Keshniya Python Multithreading--Part3 when multiple thread calling one method of code simulteniously then Lock(), acquire() and release() method will do synchronisation.... """ from threading import * class Myclass:     def __init__(self,totalTicket):         self.totalTicket=totalTicket         self.lock=Lock()  # locking     def bookTicket(self,NeedToBookTicket):         self.lock.acquire()  # locking start         print(f"Total Ticket Required by Thread/Customer Name{current_thread().name} is :{NeedToBookTicket}")         if(NeedToBookTicket<=self.totalTicket):             print(f"Ticket Boocked for Thread/Customer: {current_thread().name}")             self.totalTicket -=1         else: ...

Python Multithreading--Part2 (Overriding run Method of Thread class)

 # -*- coding: utf-8 -*- """ Created on Fri Sep 30 14:01:03 2022 @author: Mahesh.Keshniya Python Multithreading--Part2 Every thread Call "Run" method of "Thread" class through start() method but In this example we override Run method of Thread(BASE class) by Child class Run Method.... Hence If create thread and call start function than start function call Run of child class """ from threading import Thread, current_thread class Myclass(Thread):     def run(self):         print("I am run method of child thread", current_thread().name) if __name__=="__main__" :     print("I am Main thread name:",current_thread().name)     t=Myclass()     t.start()     t.join()
 # -*- coding: utf-8 -*- """ Created on Fri Sep 30 14:01:03 2022 @author: Mahesh.Keshniya This program demo for multithreading....how two different task can run simulteniously Python Multithreading--Part1 """ from threading import Thread, current_thread import time def fn_cube(c):    print("\n from cube")    for i in c :            time.sleep(0.2)            y=(i**3)            print("\ncube",y)            print("child2-----",current_thread().name) def fn_square(c):     print("\n from square")     for i in c :            time.sleep(0.2)            y=(i**2)            print("\nSquare",y)            print("child1-----",current_thread().name) if __name__ == "__main__":     print("Main thread-----",cu...
Socket Server-Client Programming in Python Client Side...  #================================================================ import socket #=========================================================================================== DISCONNECTED_MSG = "!close" SIZE = 1034 IP = socket.gethostbyname(socket.gethostname())  # getting IP address it is str type PORT = 9999 FORMAT = 'utf-8' ADDR = (IP, PORT)  # tuple type #=========================================================================================== # creating socket and binding and listening def fn_clientConnect(nIP=IP, nPort=PORT):     # print("main display" +IP)     global client     print("[Starting] Client is starting")     client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)     client.connect(ADDR)     print(f"[Connected Client ....{nIP}:{nPort}]")     connected = True     while connected:         msg = i...
Socket Server-Client Programming..... Server Side  #=========================================================================================== import socket import threading import sys #=========================================================================================== DISCONNECTED_MSG = "!close" SIZE = 1024 IP = socket.gethostbyname(socket.gethostname()) # getting IP address it is str type PORT = 9999 ADDR = (IP, PORT) #tuple type FORMAT = 'utf-8' #=========================================================================================== #creating socket and binding and listening def fn_socketBindListening(nIP=IP, nPort=PORT):     #print("main display" +IP)     print("[Starting] Server is starting")     global server     try:         server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)         server.bind(ADDR)         server.listen(1)         pri...