# 什么是线程:
线程是真正的执行工作单位
# 线程与进程的区别
1. 开进程的开销远大于开线程(生成全新的地址空间,子进程的全局变量与主进程不共用)
2. 同一进程内的多个线程共享进程的地址空间
3. 仅一个pid ,线程不能get ppid
# 方法一
import time
import random
from threading import Thread
def hook(name):
print('%s hooking' % name)
time.sleep(random.randrange(1, 5))
print('%s hooking finished' % name)
if __name__ == '__main__':
t1 = Thread(target=hook, kwargs={'name': 'egon'})
t1.start()
print('main thread')
# 方法二
import time
import random
from threading import Thread
class MyThread(Thread):
def __init__(self, name):
super(MyThread, self).__init__()
self.name = name
def run(self):
name = self.name
print('%s hooking' % name)
time.sleep(random.randrange(1, 5))
print('%s hooking finished' % name)
if __name__ == '__main__':
t1 = MyThread('egon')
t1.start()