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

"Child": Myfunc Range Print

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)
29 views7 pages

"Child": Myfunc Range Print

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
You are on page 1/ 7

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=myobj.myfunc)
thread1.start()
thread1.join()
print("done")

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

import threading
from threading import*
import time
def scr(n):
for x in n:
time.sleep(1)
print('remainer after dividing by 2' ,x%2)
def cube(n):
for x in n:
time.sleep(1)
print('remainder after dividing by 3',x%3)
n=[1,2,3,4,5,6,7,8]
start=time.time()
t1=Thread(target=scr,args=(n,))
t2=Thread(target=cube,args=(n,))
t1.start()
time.sleep(1)
t2.start()
t1.join()
t2.join()
end=time.time()
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 = threading.Thread(target=print_square,args=(10,))
t2 = threading.Thread(target=print_cube, args=(10,))

t1.start()
t2.start()
t1.join()
t2.join()

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(os.getid()))

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

if __name__ == "__main__":

print("ID of process running main program: {}".format(os.getpid()))

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


)

t1 = threading.Thread(target=task1, name='t1')
t2 = threading.Thread(target=task2, name='t2')

t1.start()
t2.start()

t1.join
t2.join

import socket #for sockets


import sys #for exit

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

self.id=id
self.name=name

def info(self):
print("Employee ID is",self.id,"and name is",self.name)

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

emp=Employee(100223,"srinithy",)
emp.info()

emp.department

class Person:
def __init__(self, personName, personAge):
self.name = personName
self.age = personAge

def showName(self):
print(self.name)

def showAge(self):
print(self.age)

class Student:
def __init__(self, studentId):
self.studentId = studentId

def getId(self):
return self.studentId

class Resident(Person, Student):


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

resident1 = Resident('RAM',28,'102')
resident1.showName()
print(resident1.getId())

class Circle:
pi=2.14

def __init__(self,radius):
self.radius=radius

def calculate_area(self):
print("Area of circle :",self.pi * self.radius * self.radius)

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

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):
self.name = name
self.max_speed = max_speed
self.mileage = 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