《零基础学Python》Python中的进程和线程【十七】

整体文章目录

整体目录

一、 当前章节目录

在这里插入图片描述

二、进程和线程

2.1 进程和线程的概念

  1. 进程:正在执行的程序,为多任务操作系统中执行任务的基本单元,是包含了程序指令和相关资源的集合。
  2. 线程:线程是进程的执行单元。
  3. 进程和线程的对比:
    - 进程是重量级的。具体包括进程映像的结构、执行细节以及进程间切换的方法。
    - 线程是轻量级的。线程之间共享许多资源,容易进行通信,生成一个线程的开销较小。

2.2 Python中对于进程和线程处理的支持

在这里插入图片描述
在这里插入图片描述

三、Python下的进程编程

3.1 进程运行环境

import os

path = os.environ.get('PATH')
print(path)
import os

for key in os.environ.keys():
    print(key, '\t', os.environ[key])
import os

os.environ['key'] = 'value'

3.2 创建进程

  1. system函数
import os

print(os.system("dir"))
  1. exec家族函数
import os

notepad = 'C:\\windows\\notepad.exe'
os.execl(notepad, 'notepad.exe')

3.3 终止进程

import sys

try:
    filename = sys.argv[1]
    print(filename)
except:
    print("Usage:", sys.argv[0], "filename")
    sys.exit(1)

四、使用subprocess模块管理进程

4.1 使用Popen类管理进程

import subprocess

pingP = subprocess.Popen(args= ['ping', 'www.sina.com.cn', '-n', '4'], shell=True)
                                # 生成ping进程
print(pingP.pid)                # 打印进程ID
print(pingP.returncode)         # 打印进程返回值

运行结果:

C:\Users\hchlcomputer>python one.py
18452
None

C:\Users\hchlcomputer>
正在 Ping ww1.sinaimg.cn.w.alikunlun.com [112.92.118.217] 具有 32 字节的数据:
来自 112.92.118.217 的回复: 字节=32 时间=22ms TTL=55
来自 112.92.118.217 的回复: 字节=32 时间=18ms TTL=55
来自 112.92.118.217 的回复: 字节=32 时间=18ms TTL=55
来自 112.92.118.217 的回复: 字节=32 时间=17ms TTL=55
112.92.118.217 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 17ms,最长 = 22ms,平均 = 18ms

import subprocess

pingP = subprocess.Popen(args=['ping', 'www.sina.com.cn', '-n', '4'], shell=True)
                                # 生成ping进程
pingP.wait()                    # 等待进程完成
print(pingP.pid)                # 打印进程ID
print(pingP.returncode)         # 打印进程返回值

运行结果:

PS C:\Users\hchlcomputer> python two.py

正在 Ping ww1.sinaimg.cn.w.alikunlun.com [112.92.118.217] 具有 32 字节的数据:
来自 112.92.118.217 的回复: 字节=32 时间=16ms TTL=55
来自 112.92.118.217 的回复: 字节=32 时间=17ms TTL=55
来自 112.92.118.217 的回复: 字节=32 时间=17ms TTL=55
来自 112.92.118.217 的回复: 字节=32 时间=17ms TTL=55

112.92.118.217 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 16ms,最长 = 17ms,平均 = 16ms
15868
0

import subprocess

pingP = subprocess.Popen(args=['ping', 'www.sina.com.cn', '-n', '4'], shell=True, stdout=subprocess.PIPE)
                                # 生成ping进程
pingP.wait()                    # 等待进程完成
print(pingP.stdout.read())      # 读取进程的输出信息
print(pingP.pid)                # 打印进程ID
print(pingP.returncode)         # 打印进程返回值

运行结果:

20632
0

4.2 调用外部系统命令

import subprocess

retcode = subprocess.call(["ls", "-1"])     # 调用ls -l命令

4.3 替代其他进程创建函数

import os
import subprocess

sts = os.system("cmd")
p = subprocess.Popen("cmd", shell=True)
sts = os.wait(p.pid, 0)

try:
    returncode = subprocess.call("cmd", shell=True)
    if returncode < 0:
        print("Child was terminated by signal", -returncode)    # 子进程被信号中断
    else:
        print("Child returned with code", returncode)       # 子进程正常返回
except OSError(e):
    print("Execution failed:", e)       # 发生了异常

五、进程间的信号机制

5.1 信号的处理

import subprocess
import signal

def sigint_handler(signum, frame):      # SIGINT信号处理函数
    print("In signal SIGINThandler")
signal.signal(signal.SIGINT, sigint_handler)      # 设置SIGINT信号处理函数

pingP = subprocess.Popen(args=['ping', 'www.sina.com.cn', '-n', '4'], shell=True)
pingP.wait()        # 等待子进程完成,后面在这里会被中断
print(pingP.pid)
print(pingP.returncode)

运行结果:

正在 Ping ww1.sinaimg.cn.w.alikunlun.com [112.92.118.221] 具有 32 字节的数据:
来自 112.92.118.221 的回复: 字节=32 时间=15ms TTL=55
来自 112.92.118.221 的回复: 字节=32 时间=21ms TTL=55
来自 112.92.118.221 的回复: 字节=32 时间=15ms TTL=55
来自 112.92.118.221 的回复: 字节=32 时间=15ms TTL=55

112.92.118.221 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 15ms,最长 = 21ms,平均 = 16ms
9680
0

5.2 信号使用的规则

  1. 信号的支持和实现在不同的系统上是不一样的,所以有些程序在不同的系统上的表现可能会不一样。
  2. 除了SIGCHLD信号(如果信号集支持)以外,信号处理函数一旦被设置后就不会改变,除非显式地重新设置。
  3. 没有办法在临界区临时屏蔽掉信号。
  4. 尽管信号是一种异步的信息传递机制,但是实际上在进行长时间计算的时候使用信号,可能会有一定的延时。
  5. 当程序在执行I/O操作的时候收到信号中断,有可能使得在信号处理函数执行完毕后,触发异常,或者直接触发异常。
  6. Python语言中的信号是使用C语言实现的,而C的信号处理函数总是会返回的。
  7. Python语言已经为部分信号注册了处理函数,如在前面的SIGINT信号,默认情况下,就会转化为KeyboardInterrupt异常。
  8. 当信号和线程同时使用的时候,必须要小心。

六、多线程概述

6.1 什么是多线程

  1. 多线程使得系统可以在单独的进程中执行并发任务。
  2. 线程是“轻量级”的,一个进程中的线程使用同样的地址空间,且共享许多资源。
  3. 多线程对于那些I/O受限的程序特别适用。

6.2 线程的状态

  1. 就绪状态:线程已经获得了除CPU外的其他资源,正在参与调度,等待被执行。当被调度选中后,将立即执行。
  2. 运行状态:占用CPU资源,正在系统中运行。
  3. 睡眠状态:暂时不参与调度,等待特定事件发生,如I/O事件。
  4. 中止状态:线程已经运行结束,等待系统回收其线程资源。

6.3 Python中的线程支持

  • Python语音中已经为各种平台提供了多线程处理能力,包括Windows、Linux等系统平台。
  • 多线程设计的最大问题是如何协调多个线程。
  • 由于大部分程序并不需要有多线程处理的能力,所以在Python启动的时候,并不支持多线程。

七、生成和终止线程

7.1 使用_thread模块

import _thread
import time

def worker(index, create_time):                                 # 具体的线程
    print("%s %s\n" % ((time.time()-create_time), index))
    print("Thread %d exit...\n" % (index))

for index in range(5):
    _thread.start_new_thread(worker, (index, time.time()))      # 启动线程

print("Main thread exit...")
while 1:
    pass

第一次运行结果:

Main thread exit…
0.007050514221191406 1
0.007050514221191406 2
0.007257699966430664 3
Thread 3 exit…
0.007257699966430664 4 Thread 2 exit…
Thread 1 exit…
0.022469758987426758 0
Thread 4 exit…
Thread 0 exit…

第二次运行结果:

Main thread exit…
0.0070552825927734375 1
0.0070552825927734375 4
Thread 4 exit…
0.0070552825927734375 0
0.02263617515563965 2
Thread 2 exit…
0.038427114486694336 3
Thread 0 exit…
Thread 1 exit…
Thread 3 exit…

import _thread
import time

def worker(index, create_time):                                 # 具体的线程
    time.sleep(1)       # 休眠1秒钟
    print("%s %s\n" % ((time.time()-create_time), index))
    print("Thread %d exit...\n" % (index))

for index in range(5):
    _thread.start_new_thread(worker, (index, time.time()))      # 启动5个线程

print("Main thread exit...")
while 1:
    pass

第一次运行结果:

Main thread exit…
1.0307164192199707 3
Thread 3 exit…
1.0458052158355713 4
1.0458052158355713 1
1.0458052158355713 2
Thread 1 exit…
Thread 2 exit…
Thread 4 exit…
1.0932824611663818 0
Thread 0 exit…

第二次运行结果:

Main thread exit…
1.0373508930206299 3
Thread 3 exit…
1.0373508930206299 2
Thread 2 exit…
1.0373508930206299 4
Thread 4 exit…
1.0373508930206299 1
1.052522897720337 0
Thread 1 exit…
Thread 0 exit…
在这里插入图片描述

7.2 使用threading.Thread类

import threading

class ThreadSkeleton(threading.Thread):
    def __init__(self):     # 线程构造函数
        threading.Thread.__init__(self)
    def run(self):
        pass

thread = ThreadSkeleton()
thread.start()
import threading
import time

class ThreadDemo(threading.Thread):
    def __init__(self, index, create_time):     # 线程构造函数
        threading.Thread.__init__(self)
        self.index = index
        self.create_time = create_time
    def run(self):              # 具体的线程运行代码
        time.sleep(1)           # 休眠1秒钟
        print("%s %s\n" % ((time.time() - self.create_time), self.index))
        print("Tread %d exit...\n" % (self.index))

for index in range(5):
    thread = ThreadDemo(index, time.time())
    thread.start()              # 启动线程
print("Main thread exit...")

运行结果:

Main thread exit…
1.0049183368682861 2
Tread 2 exit…
1.0039117336273193 4
1.0039117336273193 3
Tread 3 exit…
1.006861925125122 0
1.0059168338775635 1
Tread 0 exit… Tread 1 exit…
Tread 4 exit…

在这里插入图片描述

# 还可以通过这种方法设置线程名字
class ThreadDemo(threading.Thread):
    def __init__(self, index, create_time, thread_name):     # 线程构造函数
        threading.Thread.__init__(self, name=thread_name)
        self.index = index
        self.create_time = create_time

八、管理线程

8.1 线程状态转移

在这里插入图片描述

8.2 主线程对子线程的控制

import threading
import time

class ThreadSkeleton(threading.Thread):
    def __init__(self, index, create_time):     # 线程构造函数
        threading.Thread.__init__(self)
        self.index = index
        self.create_time = create_time
    def run(self):              # 具体的线程运行代码
        time.sleep(1)           # 休眠1秒钟
        print("%s %s\n" % ((time.time() - self.create_time), self.index))
        print("Tread %d exit...\n" % (self.index))
threads = []
for index in range(5):
    thread = ThreadSkeleton(index, time.time())
    thread.start()              # 启动线程
    threads.append(thread)

for thread in threads:
    thread.join()       # 等待线程完成

print("Main thread exit...")

运行结果:

1.01442289352417 2
1.0154485702514648 1
Tread 1 exit…
1.0164852142333984 0
Tread 0 exit…
1.0144855976104736 3
1.0144855976104736 4
Tread 4 exit…
Tread 3 exit…
Tread 2 exit…
Main thread exit…

8.3 线程中的局部变量

import threading
import random, time

class ThreadLocal():
    def __init__(self):
        self.local = threading.local()      # 生成local数据对象

    def run(self):
        time.sleep(random.random())     # 随机休眠时间
        self.local.number = []
        for i in range(10):
            self.local.number.append(random.choice(range(10)))
        print(threading.currentThread(), self.local.number)

threadLocal = ThreadLocal()
threads = []
for i in range(5):
    t = threading.Thread(target=threadLocal.run)
    t.start()   # 启动线程
    threads.append(t)
for i in range(5):
    threads[i].join()   # 等待线程完成

运行结果:

<Thread(Thread-3, started 5264)> [2, 6, 4, 7, 3, 1, 6, 0, 1, 2]
<Thread(Thread-1, started 10468)> [9, 1, 1, 1, 5, 5, 9, 6, 4, 0]
<Thread(Thread-2, started 2324)> [2, 3, 0, 7, 6, 1, 6, 6, 9, 9]
<Thread(Thread-4, started 2340)> [4, 1, 6, 1, 2, 9, 8, 2, 1, 1]
<Thread(Thread-5, started 18732)> [0, 6, 7, 9, 2, 4, 7, 0, 0, 5]

九、线程之间的同步

9.1 临界资源和临界区

  • 临界资源:一次只允许一个线程访问的资源,包括如打印机一类的硬件资源和互斥变量一类的软件资源。对临界资源的共享只能采用互斥的方式。
  • 临界区:线程中访问临界资源的代码部分。
import threading
import time

class Counter:  # 计数器类
    def __init__(self):
        self.value = 0
    def increment(self):
        self.value = self.value + 1 # 将value值加1
        value = self.value  # 并返回这个value值
        return value

counter = Counter()

class ThreadDemo(threading.Thread):
    def __init__(self, index, create_time):  # 线程构造函数
        threading.Thread.__init__(self)
        self.index = index
        self.create_time = create_time
    def run(self):
        time.sleep(1)
        value = counter.increment()
        print("%s %s values:%d \n" % (self.index, (time.time()-self.create_time), value))

for index in range(100):    # 将生成100个线程
    thread = ThreadDemo(index, time.time())
    thread.start()  # 启动线程

运行结果:

12 1.0025830268859863 values:1
13 1.0015430450439453 values:2
14 1.0015430450439453 values:3
7 1.0045711994171143 values:4
10 1.003563642501831 values:5
…省略部分输出
50 1.0118186473846436 values:93
74 1.0077881813049316 values:94
64 1.008814811706543 values:95
53 1.0108163356781006 values:96
41 1.01261305809021 values:97
98 1.015885591506958 values:98
99 1.015885591506958 values:100
97 1.015885591506958 values:99

在这里插入图片描述

9.2 锁机制

一种数据之间同步的简单方法就是使用锁机制。

在上述例子中执行结果并不像想象的那样,Count的值变为100,也是因为对临界区的不正确访问造成的。

# 低层次thread锁机制版本如下
import _thread

class Counter:  # 计数器类
    def __init__(self):
        self.value = 0
        self.lock = _thread.allocate_lock()
    def increment(self):
        self.lock.acquire() # 获取锁,进入临界区
        self.value = self.value + 1 # 将value值加1
        value = self.value  # 并返回这个value值
        self.lock.release() # 释放锁,离开临界区
        return value

运行结果:

38 1.0004780292510986 values:1
6 1.0086469650268555 values:2
34 1.0014755725860596 values:3
31 1.0014755725860596 values:4
29 1.002478837966919 values:5
25 1.002478837966919 values:6
22 1.0043470859527588 values:7
18 1.0063254833221436 values:8
24 1.0043470859527588 values:9
14 1.00734543800354 values:10
9 1.0089149475097656 values:11
20 1.005866527557373 values:12
11 1.0089149475097656 values:13
4 1.0112078189849854 values:14
3 1.0112078189849854 values:15
…省略部分输出
73 1.0112996101379395 values:94
41 1.0196380615234375 values:95
83 1.0091760158538818 values:96
44 1.0186412334442139 values:97
60 1.0139191150665283 values:98
88 1.009580135345459 values:99
76 1.0115835666656494 values:100


# 高层次threading锁机制版本如下
class Counter:  # 计数器类
    def __init__(self):
        self.value = 0
        self.lock = threading.Lock()
    def increment(self):
        self.lock.acquire() # 获取锁,进入临界区
        self.value = self.value + 1 # 将value值加1
        value = self.value  # 并返回这个value值
        self.lock.release() # 释放锁,离开临界区
        return value

运行结果:

16 1.000798225402832 values:1
12 1.0017967224121094 values:2
14 1.0017967224121094 values:3
15 1.0017967224121094 values:4
17 1.000798225402832 values:5
…省略部分输出
25 1.0201258659362793 values:96
86 1.0125746726989746 values:97
67 1.0155699253082275 values:98
87 1.0125746726989746 values:99
52 1.0166661739349365 values:100

9.3 条件变量

  • 虽然锁机制可以解决一些数据同步问题,但是这只是最低层次的同步。当线程变多,且关系变复杂的时候,就需要更加高级的同步机制了。
  • 使用“条件变量”可以使得只有在特定的条件下才对临界区进行访问。
  • 条件变量通过允许线程阻塞和等待线程发送信号的方式弥补了锁机制中的锁状态不足的问题。
  • 生产者-消费者问题:生产者线程生产产品,而同时,消费者线程消耗产品。当消费者消费产品的时候,如果没有产品,则消费者线程将阻塞,直到生产者线程生产出产品。
from threading import Thread, Condition, currentThread
import time

class Goods:        # 产品类
    def __init__(self):     # 初始化函数
        self.count=0
    def produce(self, num=1):       # 产品增加
        self.count += num
    def consume(self):      # 产品减少
        if self.count >= 0:
            self.count -= 1
    def empty(self):        # 判断产品是否为空
        return self.count <=0

class Producer(Thread):     # 生产者类
    def __init__(self, condition, goods, sleeptime=1):
        Thread.__init__(self)
        self.cond = condition
        self.goods = goods
        self.sleeptime = sleeptime

    def run(self):
        cond = self.cond
        goods = self.goods
        while True:
            cond.acquire()
            goods.produce()
            print("产品数量:", goods.count, "生产者线程")
            cond.notifyAll()        # 通知满足此条件变量的线程
            cond.release()
            time.sleep(self.sleeptime)

class Consumer(Thread):     # 消费者类
    def __init__(self, index, condition, goods, sleeptime=4):
        Thread.__init__(self, name=str(index))
        self.cond = condition
        self.goods = goods
        self.sleeptime = sleeptime
    def run(self):
        cond = self.cond
        goods = self.goods
        while True:
            time.sleep(self.sleeptime)
            cond.acquire()
            while goods.empty():
                cond.wait()     # 如果为空,则等待
            goods.consume()
            print("产品数量:", goods.count, "消费者线程", currentThread().getName())
            cond.release()

goods = Goods()
cond = Condition()

producer = Producer(cond, goods)
producer.start()            # 启动生产者线程
for i in range(5):
    consumer = Consumer(i, cond, goods)
    consumer.start()        # 启动5个消费者线程

运行结果:

产品数量: 1 生产者线程
产品数量: 2 生产者线程
产品数量: 3 生产者线程
产品数量: 4 生产者线程
产品数量: 3 消费者线程 2
产品数量: 2 消费者线程 3
产品数量: 1 消费者线程 1
产品数量: 0 消费者线程 4
产品数量: 1 生产者线程
产品数量: 0 消费者线程 0
产品数量: 1 生产者线程
产品数量: 2 生产者线程
产品数量: 3 生产者线程
产品数量: 2 消费者线程 2
产品数量: 1 消费者线程 3
产品数量: 0 消费者线程 4
产品数量: 1 生产者线程
产品数量: 0 消费者线程 1
产品数量: 1 生产者线程
产品数量: 0 消费者线程 0
产品数量: 1 生产者线程
产品数量: 2 生产者线程
产品数量: 1 消费者线程 4
产品数量: 0 消费者线程 2
…省略部分输出

9.4 信号量

信号量主要用在需要对有限的资源进行同步的时候。

# 假设资源的数目为5,可以使用如下语句来构造信号量
max_resource = 5
res_sema = Semaphore(value = max_resource)
# 当有多个线程需要使用此资源的时候,则在每次使用资源的前后加上对信号量的操作。
res_sema.acquire()
# 使用此资源
res_sema.release()

9.5 同步队列

同步队列是一个专门为多线程访问所设计的数据结构,能够有效地实现线程对资源的访问。

import threading, queue
import time, random

class Worker(threading.Thread):     # 工作类
    def __init__(self, index, queue):       # 构造函数
        threading.Thread.__init__(self)
        self.index = index
        self.queue = queue
    def run(self):
        while 1:
            time.sleep(random.random())
            item = self.queue.get()     # 从同步队列中获取对象
            if item is None:        # 循环终止条件
                break
            print("index:", self.index, "task", item, "finished")
            self.queue.task_done()

q = queue.Queue(0)      # 生成一个不限制长度的同步队列
for i in range(2):
    Worker(i, q).start()        # 生成两个线程
for i in range(10):
    q.put(i)        # 向同步队列中加入对象
for i in range(2):
    q.put(None)

运行结果:

index: 0 task 0 finished
index: 1 task 1 finished
index: 1 task 2 finished
index: 0 task 3 finished
index: 0 task 4 finished
index: 1 task 5 finished
index: 0 task 6 finished
index: 1 task 7 finished
index: 1 task 8 finished
index: 0 task 9 finished

9.6 线程同步小结

在这里插入图片描述

十、习题

习题:

  1. 什么是进程?什么是线程?
  2. 怎么使用Python创建多进程与多线程?
  3. Python中的GIL是什么?它对Python有什么影响?
  4. 使用多进程(多线程)编写一个快速排序的程序。

答案:

进程:正在执行的程序,为多任务操作系统中执行任务的基本单元,是包含了程序指令和相关资源的集合。
线程:线程是进程的执行单元。

  1. 多进程代码如下,多线程见 七、生成和终止线程的_thread模块和threading.Thread类

GIL是全局解析器锁,单核的情况下可以实现多任务(并发)。
GIL无疑就是一把全局排他锁。毫无疑问全局锁的存在会对多线程的效率有不小影响。甚至就几乎等于Python是个单线程的程序。

  1. 代码如下。
# 2. 创建多进程
# 如果要启动大量的子进程,可以用进程池(Pool)的方式批量创建子进程
from multiprocessing import Pool  # 跨平台版本的多进程模块
import _thread
import time

def worker(index, create_time):                                 # 具体的线程
    print("%s %s\n" % ((time.time()-create_time), index))
    print("Child process %d exit...\n" % (index))

if __name__ == "__main__":
    start = time.time()
    # 创建多进程
    p = Pool(6)  # 可以同时跑6个进程;Pool的默认大小是CPU的核数
    for index in range(5):
        p.apply_async(worker, (index, time.time()))      # 启动线程
    print("Child process will start")
        # 对Pool对象调用join()方法会等待所有子进程执行完毕,调用join()之前必须先调用close(),调用close()之后就不能继续添加新的Process了。
    p.close()
    p.join()
    print("Child process end")
    print('共耗时:', time.time() - start)

运行结果:

Child process will start
0.0010237693786621094 0
Child process 0 exit…
0.0010237693786621094 1
Child process 1 exit…
0.0010237693786621094 2
Child process 2 exit…
0.0010237693786621094 3
Child process 3 exit…
0.0010237693786621094 4
Child process 4 exit…
Child process end
共耗时: 0.5348596572875977


# 线程排序
from threading import Thread
from queue import Queue
import random


def xc1():
    q.put(lst)


def xc2(ff):
    lst1 = q.get()
    if ff == 'a':
        for i in range(len(lst1) - 1):
            for j in range(len(lst1) - 1 - i):
                if lst1[j] > lst1[j + 1]:
                    lst1[j], lst1[j + 1] = lst1[j + 1], lst1[j]
        print("冒泡排序结果为: ", lst1)

    elif ff == 'b':
        i = 1
        while i < len(lst1):
            t = lst1[i]
            j = i - 1
            while j >= 0 and lst1[j] > t:
                lst1[j + 1] = lst1[j]
                j -= 1
                lst1[j + 1] = t
            i += 1
        print("插入排序结果为: ", lst1)

    elif ff == 'c':
        i = 0
        while i < len(lst1) - 1:
            min = i
            j = i + 1
            while j < len(lst1):
                if lst1[j] < lst1[min]:
                    min = j
                j += 1
            if min != i:
                lst1[i], lst1[min] = lst1[min], lst1[i]
            i += 1
        print("选择排序结果为: ", lst1)

    elif ff == 'd':
        def patition(lst1, low, high):
            key = lst1[low]
            while low < high:
                while low < high and lst1[high] >= key:
                    high -= 1
                lst1[low] = lst1[high]
                while low < high and lst1[low] <= key:
                    low += 1
                lst1[high] = lst1[low]
            lst1[low] = key
            return low

        def dgdy(lst1, low, high):
            if low < high:
                mid = patition(lst1, low, high)
                dgdy(lst1, low, mid - 1)
                dgdy(lst1, mid + 1, high)

        dgdy(lst1, 0, 9)
        print("快速排序结果为: ", lst1)


if __name__ == '__main__':
    lst = []
    for i in range(10):
        lst.append(random.randint(1, 101))
    # print(lst)
    q = Queue()
    t1 = Thread(target=xc1)
    t1.start()
    ff = input("请选择需要的排序方法:冒泡排序(A) 插入排序(B) 选择排序(C) 快速排序(D)")
    ff = ff.lower()
    t2 = Thread(target=xc2, args=(ff,))
    t2.start()

运行结果:

请选择需要的排序方法:冒泡排序(A) 插入排序(B) 选择排序(C) 快速排序(D)D
快速排序结果为: [5, 8, 20, 55, 75, 77, 84, 91, 91, 99]

# 进程排序
from multiprocessing import Process, Queue
import random


def jc1(q):
    lst = []
    for i in range(10):
        lst.append(random.randint(1, 100))
    print("排序前: ", lst)
    q.put(lst)


def jc2(q):
    lst1 = q.get()
    for i in range(len(lst1) - 1):
        for j in range(len(lst1) - 1 - i):
            if lst1[j] > lst1[j + 1]:
                lst1[j], lst1[j + 1] = lst1[j + 1], lst1[j]
    print("排序后: ", lst1)


if __name__ == '__main__':
    q = Queue()
    p1 = Process(target=jc1, args=(q,))
    p2 = Process(target=jc2, args=(q,))
    p1.start()
    p2.start()

运行结果:

排序前: [8, 69, 27, 59, 52, 72, 7, 96, 95, 41]
排序后: [7, 8, 27, 41, 52, 59, 69, 72, 95, 96]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值