什么是进程(process)

正在进行的一个过程或者说一个任务。而负责执行任务则是cpu。

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: threading
# time: 2021/03/29

import time
import threading

begin = time.time()

def add(n):
    sum = 0
    for i in range(n):
        sum += i
    print(sum)

add(10000000)
add(20000000)

end = time.time()
print(end - begin)

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/process_threading.py
49999995000000
199999990000000
1.3920490741729736

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: threading
# time: 2021/03/29

import time
import threading

begin = time.time()

def add(n):
    sum = 0
    for i in range(n):
        sum += i
    print(sum)

t1 = threading.Thread(target=add,args=(10000000,))
t1.start()

t2 = threading.Thread(target=add,args=(20000000,))
t2.start()

t1.join()
t2.join()

end = time.time()
print(end - begin)    cpu执行多线程并行处理存在切换消耗,在python2.x版本串行比并行更快。

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/process_threading.py
49999995000000
199999990000000
1.6875109672546387

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: threading
# time: 2021/03/29

import time
import threading

begin = time.time()

def add(n):
    sum = 0
    for i in range(n):
        sum += i
    print(sum)

add(50000000)
add(80000000)

# t1 = threading.Thread(target=add,args=(10000000,))
# t1.start()
#
# t2 = threading.Thread(target=add,args=(20000000,))
# t2.start()
#
# t1.join()
# t2.join()

end = time.time()
print(end - begin)

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/process_threading.py
1249999975000000
3199999960000000
6.044143915176392

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: threading
# time: 2021/03/29

import time
import threading

begin = time.time()

def add(n):
    sum = 0
    for i in range(n):
        sum += i
    print(sum)

# add(50000000)
# add(80000000)

t1 = threading.Thread(target=add,args=(50000000,))
t1.start()

t2 = threading.Thread(target=add,args=(80000000,))
t2.start()

t1.join()
t2.join()

end = time.time()
print(end - begin)

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/process_threading.py
1249999975000000
3199999960000000
7.608842611312866

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: threading
# time: 2021/03/29

import time
import threading

begin = time.time()

def add(n):
    sum = 0
    for i in range(n):
        sum += i
    print(sum)

add(100000000)
add(100000000)

# t1 = threading.Thread(target=add,args=(50000000,))
# t1.start()
#
# t2 = threading.Thread(target=add,args=(80000000,))
# t2.start()
#
# t1.join()
# t2.join()

end = time.time()
print(end - begin)

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/process_threading.py
4999999950000000
4999999950000000
9.337844133377075

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: threading
# time: 2021/03/29

import time
import threading

begin = time.time()

def add(n):
    sum = 0
    for i in range(n):
        sum += i
    print(sum)

# add(100000000)
# add(100000000)

t1 = threading.Thread(target=add,args=(100000000,))
t1.start()

t2 = threading.Thread(target=add,args=(100000000,))
t2.start()

t1.join()
t2.join()

end = time.time()
print(end - begin)

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/process_threading.py
4999999950000000
4999999950000000
12.290422916412354

Process finished with exit code 0

#IO密集型任务或函数(单cpu并行可以加快) 计算密集型任务函数(单cpu并行时间更久)