我的账户
啄木鸟学院

专注软件测试菁英教育

亲爱的游客,欢迎!

已有账号,请

如尚未注册?

python-贺威栋-2021.01.25

[复制链接]
I5029学员认证 发表于 2021-1-25 20:17:46 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
'''进程'''
from multiprocessing import Process
import os,time
def run(name):
    print('运行子进程%s(%s)'%(name,os.getpid()))
def run2(name):
    print('运行子进程是%s(%s)'%(name,os.getpid()))
if __name__ == "__main__":
    print('当前子进程%s'%os.getpid())
    # run('test')    # 这行代码仍然在当前进程中执行
    # 创建一个新的进程
    p = Process(target=run,args=('test',))
    print('开始执行子进程')
    # 开始执行新进程
    p.start()
    # time.sleep(1)
    # 将子进程加入到主进程中
    p.join()
    print('子进程执行完毕')
    p2 = Process(target=run2,args=('test',))
    p2.start()
    p2.join()
'''进程池'''
from multiprocessing import Pool
import os,time,random
def task(name):
    print('运行任务%s(%s)'%(name,os.getpid()))
    start = time.time()
    # 随机休眠1-3
    time.sleep(random.randint(1,3))
    end = time.time()
    print('任务%s耗时为%.2f'%(name,(end-start)))
if __name__ == '__main__':
    print('主进程是%s'%os.getpid())
    # 初始化进程池
    pool = Pool(4)
    for i in range(6):
        # 将任务交给进程池去执行
        pool.apply_async(task,args=('任务%d'%i,))
    pool.close()
    pool.join()
'''线程'''
import threading,time
def task(name):
    print('线程%s正在运行...'%threading.current_thread().name)
    print(name)
if __name__ == '__main__':
    print('主线程%s'%threading.current_thread().name)
    # task()
    # 创建一个新线程
    t = threading.Thread(target=task,args=('传入一个参数',))
    t.start()
    t.join()
    print('主线程%s运行结束'%threading.current_thread().name)
'''
多线程安全问题
python 中多任务一般使用多进程来完成
因为在python中多线程并不能真正提高效率
进程可以在不同的电脑上运行——分布式
'''
import threading,time
# 银行账户
balance = 0
# 创建一个锁
lock = threading.Lock()
def change(money):
    global balance
    # 存钱
    balance = balance + money
    balance = balance - money
def run(money):
    for i in range(100000):
        # 上锁
        lock.acquire()
        try:
            change(money)
        finally:
            # 释放锁
            lock.release()
t1 = threading.Thread(target=run,args=(10,))
t2 = threading.Thread(target=run,args=(8,))
t1.start()
t2.start()
t1.join()
t2.join()
print('balance=%d'%balance)
'''
t1:
x1 = balance + 10
balance = x1
x1 = balance - 10
balance = x1
t2:
x2 = balance + 8
balance = x2
x2 = balance - 8
balance = x2
balance = 0
-----------------------------
t1.x1 = balance + 10
t2.x2 = balance + 8
t2.balance = x2     balance = 8
t1.balance = x1     balance = 10
t1.x1 = balance - 10    x1 = 0
t1.balance = x1     balance = 0
x2 = balance - 8    x2 = -8
balance = x2        balance = -8
'''
import mysql.connector
# 连接数据库
db = mysql.connector.connect(host='localhost',user='root',password='123456',port='3308'
# 获取游标
cursor = db.cursor()
# 创建数据库
cursor.execute('CREATE DATABASE JY40')
'''
ACID
Atomic    原子性
Consistenec    一致性
Isolation    隔离性
Duration    持久性
'''
import mysql.connector
# 连接到指定数据库
db = mysql.connector.connect(host='localhost',user='root',password='123456',port='3308',database='JY40')
# 获取游标
cursor = db.cursor()
# 获取一个user
# cursor.execute(
#     '''
#     CREATE TABLE USER2(
#     id INT,
#     NAME VARCHAR(20),
#     pwd VARCHAR(20)
# )
#     '''
# )
# 添加数据
cursor.execute('insert into user(name,pwd) values("tom","123")')
# 提交
db.commit()

回复

使用道具 举报

关注0

粉丝0

帖子27

发布主题
大家都在学
课堂讨论
一周热帖排行最近7x24小时热帖
关注我们
专注软件测试菁英教育

客服电话:17792550360

客服时间:9:00-21:00

卓目鸟学苑 - 专注软件测试菁英教育!( 陕ICP备2025058934号-2 )

版权所有 © 西安菁英教育科技有限公司 2023-2026