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个线程是交替的执行的

 

相关文章

浅谈python的输入输出,注释,基本数据类型

1.输入与输出 python中输入与输出函数为:print、input help() 帮助的使用:help() help(print) print(value, ..., sep=...

python局域网ip扫描示例分享

复制代码 代码如下:#!/usr/bin/python# -*- coding: utf-8 -*- from scapy.all import *from time import ct...

PyQt打开保存对话框的方法和使用详解

PyQt之打开保存对话框(QFileDialog)的方法和使用 一、控件说明 QFileDialog是用于打开和保存文件的标准对话框,继承自QDialog类。 QFileDialog在打...

python实现连连看辅助(图像识别)

个人兴趣,用python实现连连看的辅助程序,总结实现过程及知识点。 总体思路 1、获取连连看程序的窗口并前置 2、游戏界面截图,将每个一小图标切图,并形成由小图标组成的二维列表 3、对...

python图形用户接口实例详解

python图形用户接口实例详解

本文实例为大家分享了python图形用户接口实例的具体代码,供大家参考,具体内容如下 运用tkinter图形库,模拟聊天应用界面,实现信息发送. from tkinter impor...