ptyhon之路day10-进程与线程

时间:2021-07-18 22:42:19

---恢复内容开始---

1 开启线程的两种方式

#开启线程的方式一:使用替换threading模块提供的Thread
# from threading import Thread
# from multiprocessing import Process
#
# def task():
#     print('is running')
#
# if __name__ == '__main__':
#     t=Thread(target=task,)
#     # t=Process(target=task,)
#     t.start()
#     print('主')

#开启线程的方式二:自定义类,继承Thread
from threading import Thread
from multiprocessing import Process
class MyThread(Thread):
    def __init__(self,name):
        super().__init__()
        self.name=name
    def run(self):
        print('%s is running' %self.name)

if __name__ == '__main__':
    t=MyThread('egon')
    # t=Process(target=task,)
    t.start()
    print('')

 

2 线程与进程的pid

from threading import Thread
from multiprocessing import Process
import os

def task():
    print('%s is running' %os.getpid())

if __name__ == '__main__':
    # t1=Thread(target=task,)
    # t2=Thread(target=task,)
    t1=Process(target=task,)
    t2=Process(target=task,)
    t1.start()
    t2.start()
    print('',os.getpid())

 

3 多线程共享同一个进程内的资源

from threading import Thread
from multiprocessing import Process
n=100
def work():
    global n
    n=0

if __name__ == '__main__':

    # p=Process(target=work,)
    # p.start()
    # p.join()
    # print('主',n)

    t=Thread(target=work,)
    t.start()
    t.join()
    print('',n)

 

4 多线程共享同一进程内地址空间

from threading import Thread
msg_l=[]
format_l=[]
def talk():
    while True:
        msg=input('>>: ').strip()
        msg_l.append(msg)

def format():
    while True:
        if msg_l:
            data=msg_l.pop()
            format_l.append(data.upper())

def save():
    while True:
        if format_l:
            data=format_l.pop()
            with open('db.txt','a') as f:
                f.write('%s\n' %data)

if __name__ == '__main__':
    t1=Thread(target=talk)
    t2=Thread(target=format)
    t3=Thread(target=save)

    t1.start()
    t2.start()
    t3.start()

 

5 Thead对象其他相关的属性或方法

# from threading import Thread,activeCount,enumerate,current_thread
# import time
# def task():
#     print('%s is running' %current_thread().getName())
#     time.sleep(2)
#
# if __name__ == '__main__':
#     t=Thread(target=task,)
#     t.start()
#     t.join()
    # print(t.is_alive())
    # print(t.getName())
    # print(enumerate())
    # print('主')
    # print(activeCount())



#current_thread的用法
# from threading import Thread,activeCount,enumerate,current_thread
# from multiprocessing import Process
# import time
#
# def task():
#     print('%s is running' %current_thread().getName())
#     time.sleep(2)
#
# if __name__ == '__main__':
#     p=Process(target=task)
#     p.start()
#     print(current_thread())



from threading import Thread,activeCount,enumerate,current_thread
from multiprocessing import Process
import time

def task():
    print('%s is running' %current_thread().getName())
    time.sleep(2)

if __name__ == '__main__':
    t1=Thread(target=task)
    t2=Thread(target=task)
    t3=Thread(target=task)
    t1.start()
    t2.start()
    t3.start()
    print(current_thread())

#强调:主线程从执行层面上代表了其所在进程的执行过程

 

6 守护线程

#先看:守护进程
#
# from multiprocessing import Process
# import time
#
# def task1():
#     print('123')
#     time.sleep(1)
#     print('123done')
#
# def task2():
#     print('456')
#     time.sleep(10)
#     print('456done')
#
# if __name__ == '__main__':
#     p1=Process(target=task1)
#     p2=Process(target=task2)
#     p1.daemon = True
#     p1.start()
#     p2.start()
#     print('主')


#再看:守护线程

from threading import Thread
import time

def task1():
    print('123')
    time.sleep(10)
    print('123done')

def task2():
    print('456')
    time.sleep(1)
    print('456done')

if __name__ == '__main__':
    t1=Thread(target=task1)
    t2=Thread(target=task2)
    t1.daemon=True
    t1.start()
    t2.start()
    print('')

 

7 GIL全局解释器锁

from threading import Thread
n=100
def task():
    print('is running')

if __name__ == '__main__':
    t1=Thread(target=task,)
    t2=Thread(target=task,)
    t3=Thread(target=task,)
    # t=Process(target=task,)
    t1.start()
    t2.start()
    t3.start()
    print('')

 

8 线程的互斥锁

# from threading import Thread,Lock
# import time
# n=100
# def work():
#     global n
#     mutex.acquire()
#     temp=n
#     time.sleep(0.1)
#     n=temp-1
#     mutex.release()
#
# if __name__ == '__main__':
#     mutex=Lock()
#     l=[]
#     start=time.time()
#     for i in range(100):
#         t=Thread(target=work)
#         l.append(t)
#         t.start()
#
#     for t in l:
#         t.join()
#     print('run time:%s value:%s' %(time.time()-start,n))

 

9 互斥锁与join的区别

 

---恢复内容结束---