美文网首页
Python 多线程编程1

Python 多线程编程1

作者: 一个扫地的垃圾 | 来源:发表于2020-03-22 14:31 被阅读0次

1 创建线程

python3之后的版本中提供了threading的模块,该模块中提供了Thread类可以用来创建线程。

t = threading.Thread(target=None, args=(), kwargs=None)

  • target:指定所创建的线程要调度的目标方法,可以传入一个自定函数用于实现功能
  • args:以元组的方式,为 target 指定的方法传递参数
  • kwargs:以字典的方式,为 target 指定的方法传递参数

threading模块中提供了几种常用的函数,获取线程信息。

# coding:utf-8
import threading
def action():
    print(threading.active_count())  # 当前活动线程的个数
    print(threading.enumerate())  # 枚举当前所有活动线程
    print(threading.current_thread())  # 正在进行的线程
if __name__ == "__main__":
    action()

输出结果:

1
[<_MainThread(MainThread, started 4489174464)>]
<_MainThread(MainThread, started 4489174464)>

下面举一个实际的例子,创建一个新的进程打印语句。

# coding:utf-8
import threading
def action():
    print("This is an added thread, number is", threading.current_thread().getName())
if __name__ == "__main__":
    t_1 = threading.Thread(target=action)  # 实例化Thread类的对象
    t_2 = threading.Thread(target=action)  # 实例化Thread类的对象
    t_1.start()  # 运行线程
    t_2.start()  # 运行线程

输出结果:

This is an added thread, number is Thread-1
This is an added thread, number is Thread-2

2 优先让线程使用CPU资源

由于多线程是按照并发方式执行程序,有可能会出现当前线程的工作未执行结束,就让新的线程加入到工作中,所以Thread实例化对象的join方法可以优先使用CPU的资源,保证当前线程工作完成。

# coding:utf-8
import threading
def action():
    print("This is an added thread, number is", threading.current_thread().getName())
if __name__ == "__main__":
    t_1 = threading.Thread(target=action)  # 实例化 Thread 类的对象
    t_2 = threading.Thread(target=action)  # 实例化 Thread 类的对象
    t_1.start()  # 运行线程
    t_1.join()  # 优先让 Thread-1 线程使用CPU资源
    t_2.start()  # 运行线程
    t_2.join()  # 优先让 Thread-2 线程使用CPU资源
    for i in range(5):
        print("This is main thread, number is", threading.current_thread().getName())

输出结果:

This is an added thread, number is Thread-1
This is an added thread, number is Thread-2
This is main thread, number is MainThread
This is main thread, number is MainThread
This is main thread, number is MainThread
This is main thread, number is MainThread
This is main thread, number is MainThread

当一个线程中使用了sleep的方法,会使得当前线程阻塞,其他线程会马上加入到工作中,使用join方法会优先在线程中优先使用CPU资源。

# coding:utf-8
import threading
import time
def action_1():
    print("T_1 start:")
    time.sleep(5)
    print("T_1 finsh")
def action_2():
    print("T_2 start:")
    print("T_2 finsh")
if __name__ == "__main__":
    t_1 = threading.Thread(target=action_1)  # 实例化 Thread 类的对象
    t_2 = threading.Thread(target=action_2)  # 实例化 Thread 类的对象
    t_1.start()  # 运行线程
    # t_1.join()  # 优先让 Thread-1 线程使用CPU资源
    t_2.start()  # 运行线程
    # t_2.join()  # 优先让 Thread-2 线程使用CPU资源

输出结果:

T_1 start:
T_2 start:
T_2 finsh
T_1 finsh

从结果中可以看出线程Thread-1执行过程中,由于使用sleep进入阻塞状态,此时线程Thread-2加入并执行完工作,最后线程Thread-1完成工作。

如果对两个线程分别使用join方法,就可以当一个线程完全执行完成之后,另一个线程才加入工作。

# coding:utf-8
import threading
import time
def action_1():
    print("T_1 start:")
    time.sleep(1)
    print("T_1 finsh")
def action_2():
    print("T_2 start:")
    print("T_2 finsh")
if __name__ == "__main__":
    t_1 = threading.Thread(target=action_1)  # 实例化 Thread 类的对象
    t_2 = threading.Thread(target=action_2)  # 实例化 Thread 类的对象
    t_1.start()  # 运行线程
    t_1.join()  # 优先让 Thread-1 线程使用CPU资源
    t_2.start()  # 运行线程
    t_2.join()  # 优先让 Thread-2 线程使用CPU资源

输出结果:

T_1 start:
T_1 finsh
T_2 start:
T_2 finsh

相关文章

  • 5-线程(补充)

    Python多线程原理与实战 目的: (1)了解python线程执行原理 (2)掌握多线程编程与线程同步 (3)了...

  • 多线程

    Python多线程原理与实战 目的: (1)了解python线程执行原理 (2)掌握多线程编程与线程同步 (3)了...

  • Python多线程编程——多线程编程中的加锁机制

    如果大家对Python中的多线程编程不是很了解,推荐大家阅读之前的两篇文章:Python多线程编程——多线程基础介...

  • Python多线程编程(一)

    1. threading模块 Python 实现多线程编程可以通过thread模块(thread模块在Python...

  • Python 并发编程简介

    1 多线程和多进程 Python语言中既有多线程编程也有多进程编程,也叫做并发编程。 多进程 把一个程序分成几个不...

  • python多线程入门之旅一

    所有代码来自python核心编程 参考python核心编程一书,学习多线程工作模式,多线程实现主要模块thread...

  • Python 并行编程

    多线程编程 Python 主要提供了包括thread、threading、Queue等多线程编程模块。thread...

  • Python爬虫(四)--多线程

    Python-Socket网络编程 1. thread模块 python是支持多线程的, 主要是通过thread和...

  • Python 3 多线程编程

    本文主要基于python 3.5实现多线程编程 1. 创建多线程 2. 多线程间的同步,lock机制 3. que...

  • Python学习笔记-第11天: 并发编程和异步编程(1)

    第十一天 并发编程和异步编程(1) 今天计划学习Python的多线程编程异步编程,学习项目及练习源码地址:GitH...

网友评论

      本文标题:Python 多线程编程1

      本文链接:https://www.haomeiwen.com/subject/omxvyhtx.html