python创建线程示例

yipeiwu_com6年前Python基础

复制代码 代码如下:

import threading
from time import sleep

def test_func(id):
    for i in range(0,5):
        sleep(1)
        print('thread %d is running %d' % (id,i))

threads = []
for i in range(0,3):
    t = threading.Thread(target=test_func, args=(i,))
    threads.append(t)

for t in threads:
    t.start()

for t in threads:
    t.join()


从输出结果可以看到,3个线程是交替的执行的

 

相关文章

matplotlib实现显示伪彩色图像及色度条

matplotlib实现显示伪彩色图像及色度条

灰度图显示为伪彩色图 法一 import matplotlib.pyplot as plt img = plt.imread('C:/Users/leex/Desktop/lena...

Python数据可视化:泊松分布详解

Python数据可视化:泊松分布详解

一个服从泊松分布的随机变量X,表示在具有比率参数(rate parameter)λ的一段固定时间间隔内,事件发生的次数。参数λ告诉你该事件发生的比率。随机变量X的平均值和方差都是λ。...

解决python3中的requests解析中文页面出现乱码问题

第一部分 关于requests库 (1) requests是一个很实用的Python HTTP客户端库,编写爬虫和测试服务器响应数据时经常会用到。 (2) 其中的Request对象在访问...

Python 内置函数complex详解

英文文档: class complex([real[, imag]]) Return a complex number with the value real + imag*1j or...

django 常用orm操作详解

Django流程: 1 创建Django项目 : django-admin startproject projectname 2 创建应用: : python manage.py sta...