我的账户
啄木鸟学院

专注软件测试菁英教育

亲爱的游客,欢迎!

已有账号,请

如尚未注册?

pythonz学习第7天_刘国平_2021.01.25

[复制链接]
果丹卷学员认证 发表于 2021-1-25 21:20:19 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
一、工作内容:9:00-20:00
1.'''线程和进程'''
#线程是程序运行的最小单元,cpu调度的最小单元
from multiprocessing import Process      #子进程
import os,time
def run(name):
    print('运行子进程%s(%s)'%(name,os.getpid()))

def run2(name):
    print('运行子进程2%s(%s)'%(name,os.getpid()))

if __name__ == '__main__':
    print('当前进程是%s'%os.getpid())

    #run('test')  #这行代码仍然在当前进程中执行
    P = Process(target=run,args=('test',))   #创建一个新的进程,函数和参数分开写,参数以元组形式表示
    P2 =Process(target=run2,args=('demo',))
    print('开始执行子进程')
    #开始执行新进程
    P.start()
    P2.start()
    #time.sleep(1)
    P.join()
    P2.join()                    #将子进程加入到主进程当中
    print('子进程执行完毕')
2.'''进程池'''
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()
3.'''线程'''
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)
4.'''多线程安全问题

GIL锁 在python中运行多任务一般建议使用多进程完成(多个cpu同时运行多个进程),因为python多线程并不能真正提高效率(同一时间执行一个线程),
另外,进程可以在不同的电脑上运行-分布式。
'''
import threading,time
#银行账户
balance = 0

#创建一个锁
lock = threading.Lock()

def change(money):
    global balance
    #存钱
    '''t1:
    计算机先计算balance+money,将结果存在临时变量x1里面,然后将临时变量x1的值赋给balance
    计算机先计算balance-money,将结果存在临时变量x1里面,然后将临时变量x1的值赋给balance
    t2:
    计算机先计算balance+money,将结果存在临时变量x2里面,然后将临时变量x2的值赋给balance
    计算机先计算balance-money,将结果存在临时变量x2里面,然后将临时变量x2的值赋给balance
    '''
    balance = balance+money
    balance = balance-money


def run(money):
    for i in range(1000000):
        #上锁
        lock.acquire()
        try:
            change(money)
        finally:
            #解锁
            lock.release()

t1 = threading.Thread(target=run,args=(10,))
t2 = threading.Thread(target=run,args=(8,))
t3 = threading.Thread(target=run,args=(100,))
t4 = threading.Thread(target=run,args=(23,))

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

t1.join()
t2.join()
t3.join()
t4.join()
print('balance=%d'%balance)
5.连接数据库
import mysql.connector

#连接数据库
db=mysql.connector.connect(host='localhost',port=3307,user='root',password='123456')

#获取游标
cursor=db.cursor()

#创建数据库
cursor.execute('create database jy40')
6.穿件表,插入数据并提交数据
import mysql.connector
#连接到指定的数据库
db=mysql.connector.connect(host='localhost',port=3307,user='root',password='123456',database='jy40')
#获取游标
cursor=db.cursor()
#添加一个user表
cursor.execute('''create table if not exists user(
    id int primary key auto_increment,
    name varchar(50) unique not null,
    pwd varchar(50) not null
    )''')
#添加数据
cursor.execute('''insert into user (name,pwd) values(
    'zhangsan','123456')''')

#提交
db.commit()
'''
ACID
Atomic 原子性(不可分割的)
Consistence   一致性
Isolation     隔离性
Duration      持久性
'''
20:00-23:00
复习mysql
二、遇到的问题:
导入mysql.connector模块时报出“No module named 'mysql'”错误
三、处理方式:
发现安装了多个python版本,将Microsoft store中的Python3.9卸载,只保留python 3.8,问题得到解决。
回复

使用道具 举报

关注0

粉丝0

帖子15

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

客服电话:17792550360

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

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

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