python 多线程,详细教程,线程同步,线程加锁,ThreadPoolExecutor

本文介绍了Python多线程的基本使用,重点讲解了线程同步和线程加锁的概念,提供了避免数据不同步的解决方案。同时,文章探讨了如何使用ThreadPoolExecutor进行线程池管理,包括在Python2.x环境下安装futures库的方法。
摘要由CSDN通过智能技术生成

python 多线程的使用

多线程主要用于大量的IO操作时的并发执行,以此来提高效率!多进程主要用于大量的计算,进而充分发挥CPU的性能!

这里主要讲述两种多线程的使用:

 

  1. threading.Thread
  2. concurrent.futures.ThreadPoolExecutor

使用第二种方式需要先安装(对于python2.x)

pip install futures

基本使用

第一种方式的简单使用:(注意:空白行有删减)

# coding=utf8
 
import requests
import threading
import concurrent
from concurrent.futures import ThreadPoolExecutor
 
def task():
    url = "https://www.baidu.com"
    res = requests.get(url)
    if res.status_code == 200:
        print "yes!"
    else:
        print "no!"
 
def main():
    t1 = threading.Thread(target=task)  # 用法和Process类很相似
    t2 = threading.Thread(target=task)
 
    t1.start()
    t2.start()
    t1.j
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值