0% found this document useful (0 votes)
9 views7 pages

Threading and Class Examples in Python

The document contains examples of using threading in Python. It shows how to create Thread objects, start threads, join threads, and pass arguments to thread functions. It also demonstrates getting thread and process IDs.

Uploaded by

Kokila
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Threading and Class Examples in Python

The document contains examples of using threading in Python. It shows how to create Thread objects, start threads, join threads, and pass arguments to thread functions. It also demonstrates getting thread and process IDs.

Uploaded by

Kokila
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

from threading import*

class ex:
def myfunc(self): #self recessary as first parameter in a class func
for x in range(7):
print("child")
myobj= ex()
thread1= Thread(target=[Link])
[Link]()
[Link]()
print("done")

import time
def scr(n):
for x in n:
[Link](1)
x%2
def cube(n):
for x in n:
[Link](1)
x%3
n=[1,2,3,4,5,6,7,8]
s=[Link]()
scr(n)
cube(n)
e=[Link]()
print(e-s)

import threading
from threading import*
import time
def scr(n):
for x in n:
[Link](1)
print('remainer after dividing by 2' ,x%2)
def cube(n):
for x in n:
[Link](1)
print('remainder after dividing by 3',x%3)
n=[1,2,3,4,5,6,7,8]
start=[Link]()
t1=Thread(target=scr,args=(n,))
t2=Thread(target=cube,args=(n,))
[Link]()
[Link](1)
[Link]()
[Link]()
[Link]()
end=[Link]()
print(end-start)

import threading
def print_cube(num):
"""
function to print cube of given num
"""
print("cube: {}".format(num * num * num))

def print_square(num):
"""
function to print square of given num
"""
print ("square: {}".format(num*num))

if __name__ == "__main__":

t1 = [Link](target=print_square,args=(10,))
t2 = [Link](target=print_cube, args=(10,))

[Link]()
[Link]()
[Link]()
[Link]()

print("Done!")

import threading
import os

def task1():
print("Task 1 assigned to thread: {}". format(threading.current_thre
ad().name))
print("ID of process running task 1:{}".format([Link]()))

def task2():
print("Task 2 assigned to thread: {}".format([Link]()))
print("Main thread name: {}". format(threading.current_thread().name
))

if __name__ == "__main__":

print("ID of process running main program: {}".format([Link]()))

print("Main thread name: {}".format(threading.current_thread().name)


)

t1 = [Link](target=task1, name='t1')
t2 = [Link](target=task2, name='t2')

[Link]()
[Link]()

[Link]
[Link]

import socket #for sockets


import sys #for exit

try:
#create an AF_INET, STREAM socket(TCP)
sock_obj = [Link](socket.AF_INET, socket.SOCK_STREAM)
except [Link] as err_msg:
print('unable to instantiate socket. error code:' + str(err_msg[0])+
', Error message:' + err_msg[1])
[Link]();
print ('socket initializred')
class Employee:
def __init__(self,id,name):

[Link]=id
[Link]=name

def info(self):
print("Employee ID is",[Link],"and name is",[Link])

def department(self):
print("Employee of IT department")

emp=Employee(100223,"srinithy",)
[Link]()

[Link]

class Person:
def __init__(self, personName, personAge):
[Link] = personName
[Link] = personAge

def showName(self):
print([Link])

def showAge(self):
print([Link])

class Student:
def __init__(self, studentId):
[Link] = studentId

def getId(self):
return [Link]

class Resident(Person, Student):


def __init__(self, name, age, id):
Person.__init__(self, name, age)
Student.__init__(self, id)

resident1 = Resident('RAM',28,'102')
[Link]()
print([Link]())

class Circle:
pi=2.14

def __init__(self,radius):
[Link]=radius

def calculate_area(self):
print("Area of circle :",[Link] * [Link] * [Link])

class Rectangle:
def __init__(self,length,width):
[Link]=length
[Link]=width
def calculate_area(self):
print("area of rectangle :",[Link] * [Link])

cir=Circle(5)
rect=Rectangle(10,5)
cir.calculate_area()

rect.calculate_area()

class ClassOne:
def func1(self):
print('we are in base class')

class ClassTwo(ClassOne):
def func2(self):
print('we are in child class')
obj = ClassTwo()
obj.func1()
obj.func2()

class Vehicle:

def __init__(self,name,max_speed,mileage):
[Link] = name
self.max_speed = max_speed
[Link] = mileage
class Bus(Vehicle):
pass
School_bus = Bus("School Volvo",180,12)
print("Vehicle Name:",School_bus.name, "Speed:", School_bus.max_speed,
"Mileage:", School_bus.mileage)

class Person:
def person_info(self,name,age):
print('Inside Person class')
print('Name:',name,'Age:',age)

class Company:
def company_info(self,company_name,location):
print('Inside Company class')
print('Name:',company_name,'location:',location)

class Employee(Person,Company):
def Employee_info(self,salary,skill):
print('Inside Employee class')
print('Salary:',salary,'Skill:',skill)

emp=Employee()
emp.person_info('Chiru',25)
emp.company_info('Google','Atlanta')
emp.Employee_info(12000,'IoT')

You might also like