首页 > 编程语言 >多线程threading in Python

多线程threading in Python

时间:2023-01-19 16:44:06浏览次数:53  
标签:do start Python threading something time print 多线程

Simple Example 1

import time
import threading

start = time.perf_counter()

def do_something():
	print('sleeping 1 second...')
	time.sleep(1)
	print('Done sleeping...')

t1 = threading.Thread(target = do_something)
t2 = threading.Thread(target = do_something)

t1.start()
t2.start()

t1.join()
t2.join()
finish = time.perf_counter()

print(f'Finish in {round(finish-start,2)} seconds(s)')

多个线程的For循环

import time
import threading

start = time.perf_counter()

def do_something():
	print('sleeping 1 second...')
	time.sleep(1)
	print('Done sleeping...')

threads = []
for _ in range(10):
	t = threading.Thread(target=do_something)
	t.start()
	threads.append(t)

for thread in threads:
	thread.join()


finish = time.perf_counter()

print(f'Finish in {round(finish-start,2)} seconds(s)')

加个参数

import time
import threading

start = time.perf_counter()

def do_something(seconds):
	print(f'sleeping {seconds} second...')
	time.sleep(seconds)
	print('Done sleeping...')


threads = []
for _ in range(10):
	t = threading.Thread(target=do_something, args = [2])
	t.start()
	threads.append(t)

for thread in threads:
	thread.join()


finish = time.perf_counter()

print(f'Finish in {round(finish-start,2)} seconds(s)')

标签:do,start,Python,threading,something,time,print,多线程
From: https://www.cnblogs.com/conpi/p/17061749.html

相关文章

  • Python - requests 使用记录
    requests使用简单方法记录importrequestsfromfake_useragentimportUserAgentua=UserAgent()headers={'User-Agent':ua.random#伪装}#......
  • 谷歌浏览器启用多线程下载
    谷歌浏览器启用多线程下载功能教程1、双击进入软件,在页面上方地址栏中输入“chrome://flags/#enable-parallel-downloading”并回车访问   2、进入新界......
  • 我的Python程序太慢了。如何加快速度?
    如果你的Python程序太慢,你可以按照下面给出的提示和技巧-抽象化避免过度抽象,尤其是在微小函数或方法的形式下。抽象往往会产生间接性,并迫使解释器工作更多。如果间接寻......
  • (转载)Python中关键词yield怎么用?
    原文:https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do译文:https://zhuanlan.zhihu.com/p/23276711?refer=passer问题描述:Python中关......
  • Python如何运行程序
    Python如何运行程序Python解释器简介解释器是一种让其他程序运行起来的程序。Python解释器将读取程序,并按照其中的命令执行,得出结果。解释器是代码与机器的计算机硬件......
  • win10下python3.9的代理报错问题解决(附web3的polygon爬虫源码)
    背景因为工作中经常需要代理访问,而开了代理,request就会报错SSLError,如下:requests.exceptions.SSLError:HTTPSConnectionPool(host='test-admin.xxx.cn',port=443):Ma......
  • python3安装
    前言由于CentOS7本身就需要安装Python2.7.5,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到。whichpythonPython3的方法首先安装依赖包yum-ygroupi......
  • python提取json中键值
    0X00 样例数据:{'d':{'__type':'st','YX':"<tableid='tabyxlist'><tr><tdclass='tdth'>院校名称</td><tdclass='tdth'>计划数</td></tr><trid=......
  • 【转载】 python鸭子类型与protocol
    版权声明:本文为CSDN博主「yuanzhoulvpi」的原创文章,遵循CC4.0BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/yuanzhoulvpi/article/deta......
  • python _XMLParser.__init__()初始化失败,提示“takes 1 positional argument but 4 we
    问题:在一个新的环境下,执行openpyxl相关的操作,初始化时,逐步执行,需要调到 ElementTree.py_XMLParser.__init__(self,html,target,encoding),但是初始化报错,【 _XMLPars......