python创建线程示例

yipeiwu_com5年前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多进程下实现日志记录按时间分割

python多进程下实现日志记录按时间分割,供大家参考,具体内容如下 原理:自定义日志handler继承TimedRotatingFileHandler,并重写computeRollov...

PyTorch和Keras计算模型参数的例子

Pytorch中,变量参数,用numel得到参数数目,累加 def get_parameter_number(net): total_num = sum(p.numel() fo...

python用quad、dblquad实现一维二维积分的实例详解

背景: python函数库scipy的quad、dblquad实现一维二维积分的范例。需要注意dblquad的积分顺序问题。 代码: import numpy as np from...

解决pycharm工程启动卡住没反应的问题

今天早上用pycharm启动django工程的时候,一直卡在如下提示: Performing system checks... System check identified no...

python使用append合并两个数组的方法

本文实例讲述了python使用append合并两个数组的方法。分享给大家供大家参考。具体如下: lista = [1,2,3] listb = [4,5,6] mergedlist...