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使用ntplib库同步校准当地时间的方法

NTP(Network Time Protocol)是由美国德拉瓦大学的David L. Mills教授于1985年提出,设计用来在Internet上使不同的机器能维持相同时间的一种通讯...

python dict.get()和dict['key']的区别详解

先看代码: In [1]: a = {'name': 'wang'} In [2]: a.get('age') In [3]: a['age'] -----------...

Python判断值是否在list或set中的性能对比分析

本文实例对比分析了Python判断值是否在list或set中的执行性能。分享给大家供大家参考,具体如下: 判断值是否在set集合中的速度明显要比list快的多, 因为查找set用到了ha...

简单实现python数独游戏

网上看到一个python写的数独,很好玩,分享给大家。 import random import itertools from copy import deepcopy def m...

python版微信跳一跳游戏辅助

python版微信跳一跳游戏辅助

本文实例为大家分享了微信跳一跳游戏辅助python代码,供大家参考,具体内容如下 import os import PIL import numpy import matplotli...