线程与进程

线程与进程:
线程是什么:线程是指进程内的一个执行单元,也是进程内的可调度实体.

与进程的区别:
(1) 地址空间:进程内的一个执行单元;进程至少有一个线程;它们共享进程的地址空间;而进程有自己独立的地址空间;
(2) 资源拥有:进程是资源分配和拥有的单位,同一个进程内的线程共享进程的资源
(3) 线程是处理器调度的基本单位,但进程不是.
(4) 二者均可并发执行.

简而言之,一个程序至少有一个进程,一个进程至少有一个线程.
线程的划分尺度小于进程,使得多线程程序的并发性高。
另外,进程在执行过程中拥有独立的内存单元,而多个线程共享内存,从而极大地提高了程序的运行效率。

线程和进程关系?
进程就是一个应用程序在处理机上的一次执行过程,它是一个动态的概念,而线程是进程中的一部分,进程包含多个线程在运行。
多线程可以共享全局变量,多进程不能。多线程中,所有子线程的进程号相同;多进程中,不同的子进程进程号不同。
进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单位.
线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位.线程自己基本上不拥有系统资源,只拥有一点在运行中必不可少的资源(如程序计数器,一组寄存器和栈),但是它可与同属一个进程的其他的线程共享进程所拥有的全部资源.
一个线程可以创建和撤销另一个线程;同一个进程中的多个线程之间可以并发执行.

python中多线程:
python主要是通过thread和threading这两个模块来实现多线程支持。python的thread模块是比较底层的模块,python的threading模块是对thread做了一些封装,可以更加方便的被使用。但是python(cpython)由于GIL的存在无法使用threading充分利用CPU资源,如果想充分发挥多核CPU的计算能力需要使用multiprocessing模块(Windows下使用会有诸多问题)。

ython3.x中通过threading模块创建新的线程有两种方法:
一种是通过threading.Thread(Target=executable Method)-即传递给Thread对象一个可执行方法(或对象);
第二种是继承threading.Thread定义子类并重写run()方法。第二种方法中,唯一必须重写的方法是run()

# (1)通过threading.Thread进行创建多线程
import threading
import time


def target():
print('当前线程“%s”正在运行' % (threading.current_thread().name))
time.sleep(1)
print("当前线程“%s”结束" % (threading.current_thread().name))

print("线程“%s”开始运行" % (threading.current_thread().name))
# 属于线程t的部分
t = threading.Thread(target=target)
t.start()
# 属于线程t的部分
t.join() # join是阻塞当前线程(此处的当前线程时主线程) 主线程直到Thread-1结束之后才结束、
print("线程“%s”结束 " % (threading.current_thread().name))


class myThread(threading.Thread): # 继承父类threading.Thread
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter

def run(self): # 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
print("开始 " + self.name)
print_time(self.name, self.counter, 5)
print("退出 " + self.name)

def print_time(threadName, delay, counter):
while counter:
time.sleep(delay)
print("%s 处理: %s" % (threadName, time.ctime(time.time())))
counter -= 1

# 创建新线程 thread1和thread2执行顺序是乱序的。要使之有序,需要进行线程同步
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
# 开启线程
thread1.start()
thread2.start()
# 等待线程结束
thread1.join()
thread2.join()
print("退出所以线程")

# 多线程实现同步有四种方式:锁机制,信号量,条件判断和同步队列。
# 我主要关注两种同步机制:锁机制和同步队列。

class myThread(threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print("开始中 " + self.name)
# 获得锁,成功获得锁定后返回True
# 可选的timeout参数不填时将一直阻塞直到获得锁定
# 否则超时后将返回False
threadLock.acquire()
print_times(self.name, self.counter, 5)
# 释放锁
threadLock.release()
def print_times(threadName, delay, counter):
while counter:
time.sleep(delay)
print("线程%s: 时间 %s" % (threadName, time.ctime(time.time())))
counter -= 1

threadLock = threading.Lock()
threads = []
# 创建新线程
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
# 开启新线程
thread1.start()
thread2.start()
# 添加线程到线程列表
threads.append(thread1)
threads.append(thread2)
# 等待所有线程完成
for t in threads:
t.join()
print("Exiting Main Thread")

# 线程同步队列queue
import queue
class Worker(threading.Thread):
def __init__(self, name, queue):
threading.Thread.__init__(self)
self.queue = queue
self.start() #执行run()

def run(self):
#循环,保证接着跑下一个任务
while True:
# 队列为空则退出线程
if self.queue.empty():
break
# 获取一个队列数据
foo = self.queue.get()
# 延时1S模拟你要做的事情
time.sleep(1)
# 打印
print('线程名称' + self.getName() + " process " + str(foo))
# 任务完成
self.queue.task_done()
# 队列
queue = queue.Queue()
# 加入100个任务队
for i in range(10):
queue.put(i)
# 开10个线程
for i in range(10):
threadName = 'Thread' + str(i)
Worker(threadName, queue)
# 所有线程执行完毕后关闭
queue.join()



posted @ 2022-08-16 20:58  李同学_学习  阅读(70)  评论(0)    收藏  举报