0% found this document useful (0 votes)
19 views

Practical Summary

The document discusses different ways to work with threads in Python including creating threads from a class and running them, getting process IDs and names, killing specific processes, working with files and folders including copying, moving, searching, and changing permissions.

Uploaded by

kareem mahmoud
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Practical Summary

The document discusses different ways to work with threads in Python including creating threads from a class and running them, getting process IDs and names, killing specific processes, working with files and folders including copying, moving, searching, and changing permissions.

Uploaded by

kareem mahmoud
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Thread in method Thread in class

import threading from threading import *

def greet_them(people): class Hello(Thread):


for person in people: def run(self):
print("Hello" + person + ".How are you?\n") for i in range(5):
print(f"hello {i}\n")

def assign_id(people):
i=0 class Hi(Thread):
for person in people: def run(self):
print("Hey!{},your id{}.\n".format(person, grades[i])) for i in range(5):
i += 1 print(f"hi {i}\n")

people = ['Richard', 'Dinesh', 'Elrich'] t1=Hello()


grades = ['a', 'D', 'e'] t2 = Hi()

t1 = threading.Thread(target=greet_them, args=(people,)) t1.start()


t2 = threading.Thread(target=assign_id, args=(people,)) t2.start()

t1.start()
t2.start()

#t2.join()
print("Woaahh!! My work is finished..")
Get Processes ID , Name Kill Processes
import os import os
import psutil import psutil

process_names = [proc.name() for proc in psutil.process_iter()] for process in psutil.process_iter():


print (process_names) if process.name() == "notepad.exe":
print(process)
pid = os.getpid() process.kill()
process_pid = psutil.Process(pid) process.memory_percent()
process_name = process_pid.name()
print (pid, process_pid, process_name)

ppid = os.getppid()
pprocess_pid = psutil.Process(ppid)
pprocess_name = pprocess_pid.name()
print (ppid, pprocess_pid, pprocess_name)
Folder Txt files Search, chmod

pwd echo “linux ubuntu” > sec3.txt grep Ubuntu section.txt


cd os cat >> sec3.txt grep Ubuntu section.txt section2.txt
cd.. save the changes press CTRL+d
rm sec3.txt
grep Ubuntu –n section.txt
mkdir os cp sec2.txt sec3.txt grep Ubuntu –v section.txt
rmdir emptyDir cp sec2.txt ~/os/sec2.txt grep Ubuntu –c section.txt
rm -r os
cp -r os ~/Desktop/os mv sec1.txt sec2.txt chmod u-rw sec.txt
cp -r os oscopy mv sec1.txt ~/ os/sec1.txt chmod u+rw sec.txt
mv sec1.txt ~/os/ sec2.txt
mv os osRename
mv os ~/ Desktop/os cat sec.txt
mv os ~/Desktop/ osRename tac sec.txt
head sec.txt
tail sec.txt
nl sec.txt
wc sec.txt

ls
Date
cal

You might also like