栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python-实用

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python-实用

python实用

装饰器浅拷贝和深拷贝

浅拷贝深拷贝 多线程

装饰器

闭包函数的语法糖。

浅拷贝和深拷贝

引用与复制
python中赋值语句都是引用。
拷贝就是复制。

浅拷贝

b = a[::] 切片复制

深拷贝
import copy
a = [0, [1, 2], 3]
b = copy.deepcopy(a)
print(a, b)
a[1][0] = 0
print(a, b)
b[1][0] = 2
print(a, b)

多线程

treading模块

import time
import threading
# 左手画圆
def draw_circle(cost):
    print("start draw a circle ", time.ctime())
    time.sleep(cost)
    print("draw a circle ", time.ctime())

# 右手画方
def draw_square(cost):
    print("start draw a squre", time.ctime())
    time.sleep(cost)
    print("draw a squre ", time.ctime())

def multi_thread():
    draw_circle_thread = threading.Thread(target=draw_circle, args=(1,))
    draw_square_thread = threading.Thread(target=draw_square, args=(2,))
    draw_circle_thread.start()
    draw_square_thread.start()

if __name__ == '__main__':
    print("start", time.ctime())
    multi_thread()
    print("end", time.ctime())



补充两个设置语句join:

import time
import threading
# 左手画圆
def draw_circle(cost):
    print("start draw a circle ", time.ctime())
    time.sleep(cost)
    print("draw a circle ", time.ctime())

# 右手画方
def draw_square(cost):
    print("start draw a squre", time.ctime())
    time.sleep(cost)
    print("draw a squre ", time.ctime())

def multi_thread():
    draw_circle_thread = threading.Thread(target=draw_circle, args=(1,))
    draw_square_thread = threading.Thread(target=draw_square, args=(2,))
    draw_circle_thread.start()
    draw_square_thread.start()
    draw_circle_thread.join() //调用join之后主线程会等待子线程完成之后才往下走
    draw_square_thread.join() //

if __name__ == '__main__':
    print("start", time.ctime())
    multi_thread()
    print("end", time.ctime())


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/757923.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号