Python-threading(一)

65 篇文章 4 订阅

看到进程和线程的区别,这里写一下:

进程:是并发执行的程序在执行过程中分配和管理资源的基本单位,是一个动态概念,竞争计算机系统资源的基本单位。

           优点:同时利用多个cpu,同时进行多个操作

           缺点:耗费资源,需同时开辟多个内存空间,

线程:是进程的一个执行单元,是进程内可调度实体。比进程更小的独立运行的基本单位。线程也被称为轻量级进程。

一个程序至少一个进程,一个进程至少一个线程。

           优点:共享内存,IO操作时,创造并发操作

           缺点:抢占资源

为什么会有线程?

  每个进程都有自己的地址空间,即进程空间,在网络或多用户换机下,一个服务器通常需要接收大量不确定数量用户的并发请求,为每一个请求都创建一个进程显然行不通(系统开销大响应用户请求效率低),因此操作系统中线程概念被引进。

  • 线程的执行过程是线性的,尽管中间会发生中断或者暂停,但是进程所拥有的资源只为改线状执行过程服务,一旦发生线程切换,这些资源需要被保护起来。
  • 进程分为单线程进程和多线程进程,单线程进程宏观来看也是线性执行过程,微观上只有单一的执行过程。多线程进程宏观是线性的,微观上多个执行操作。

线程的改变只代表CPU的执行过程的改变,而没有发生进程所拥有的资源的变化。 

进程线程的区别:

  • 地址空间:同一进程的线程共享本进程的地址空间,而进程之间则是独立的地址空间。
  • 资源拥有:同一进程内的线程共享本进程的资源如内存、I/O、cpu等,但是进程之间的资源是独立的。

     一个进程崩溃后,在保护模式下不会对其他进程产生影响,但是一个线程崩溃整个进程都死掉。所以多进程要比多线程健壮。

     进程切换时,消耗的资源大,效率高。所以涉及到频繁的切换时,使用线程要好于进程。同样如果要求同时进行并且又要共享某些变量的并发操作,只能用线程不能用进程

  • 执行过程:每个独立的进程程有一个程序运行的入口、顺序执行序列和程序入口。但是线程不能独立执行,必须依存在应用程序中,由应用程序提供多个线程执行控制。
  • 线程是处理器调度的基本单位,但是进程不是。
  • 两者均可并发执行。

优缺点:

  线程执行开销小,但是不利于资源的管理和保护。线程适合在SMP机器(双CPU系统)上运行。

  进程执行开销大,但是能够很好的进行资源管理和保护。进程可以跨机器前移。

何时使用多进程,何时使用多线程?

对资源的管理和保护要求高,不限制开销和效率时,使用多进程。

要求效率高,频繁切换时,资源的保护管理要求不是很高时,使用多线程

概括起来就是

IO密集型(不用CPU)
    多线程
计算密集型(用CPU)
    多进程
使用线程和进程的目的都是为了提升效率
(1)单进程单线程,主进程、主线程
(2)自定义线程:
        主进程
            主线程
            子线程

言归正传,先看看threading 【thread】模块提供的属性

# coding=utf-8
import threading as th


print(dir(th))
print(dir(th.Thread()))


output:

C:\Python37\python3.exe D:/PY3project/test/sshmod/likessh.py
['Barrier', 'BoundedSemaphore', 'BrokenBarrierError', 'Condition', 'Event', 'Lock', 'RLock', 'Semaphore', 'TIMEOUT_MAX', 'Thread', 'ThreadError', 'Timer', 'WeakSet', '_CRLock', '_DummyThread', '_MainThread', '_PyRLock', '_RLock', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_active', '_active_limbo_lock', '_after_fork', '_allocate_lock', '_count', '_counter', '_dangling', '_deque', '_enumerate', '_format_exc', '_islice', '_limbo', '_main_thread', '_newname', '_os', '_pickSomeNonDaemonThread', '_profile_hook', '_set_sentinel', '_shutdown','_start_new_thread', '_sys', '_time', '_trace_hook', 'activeCount', 'active_count', 'currentThread', 'current_thread', 'enumerate', 'get_ident', 'local', 'main_thread', 'setprofile', 'settrace', 'stack_size']


['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__''__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_args', '_bootstrap', '_bootstrap_inner', '_daemonic', '_delete', '_exc_info', '_ident', '_initialized', '_is_stopped', '_kwargs', '_name', '_reset_internal_locks', '_set_ident', '_set_tstate_lock', '_started', '_stderr', '_stop', '_target', '_tstate_lock', '_wait_for_tstate_lock', 'daemon', 'getName', 'ident', 'isAlive', 'isDaemon', 'is_alive', 'join', 'name', 'run', 'setDaemon', 'setName', 'start']

模块介绍

threading用于提供线程相关的操作,线程是应用程序中工作的最小单元。python当前版本的多线程库没有实现优先级、线程组,线程也不能被停止、暂停、恢复、中断。

threading模块提供的类:  
  Thread, Lock, Rlock, Condition, [Bounded]Semaphore, Event, Timer, local。

threading 模块提供的常用方法: 
  threading.currentThread(): 返回当前的线程变量。 
  threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。 
  threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。

threading 模块提供的常量:

  threading.TIMEOUT_MAX 设置threading全局超时时间。

Thread类

首先看下thread类定义

class Thread:
    _initialized = False
    _exc_info = _sys.exc_info

    def __init__(self, group=None, target=None, name=None,
                 args=(), kwargs=None, *, daemon=None):
        assert group is None, "group argument must be None for now"
        if kwargs is None:
            kwargs = {}
        self._target = target
        self._name = str(name or _newname())
        self._args = args
        self._kwargs = kwargs
        if daemon is not None:
            self._daemonic = daemon
        else:
            self._daemonic = current_thread().daemon
        self._ident = None
        self._tstate_lock = None
        self._started = Event()
        self._is_stopped = False
        self._initialized = True
        # sys.stderr is not stored in the class like
        # sys.exc_info since it can be changed between instances
        self._stderr = _sys.stderr
        # For debugging and _after_fork()
        _dangling.add(self)


#提供的方法
    def start(self) -> None: ...
    def run(self) -> None: ...
    def join(self, timeout: Optional[float] = ...) -> None: ...
    def getName(self) -> str: ...
    def setName(self, name: str) -> None: ...
    def is_alive(self) -> bool: ...
    if sys.version_info < (3,):
        def isAlive(self) -> bool: ...
    def isDaemon(self) -> bool: ...
    def setDaemon(self, daemonic: bool) -> None: ...

用示例看看这些方法的用法:

import threading as th
import time as t

def do_sth(thing):
    print ("do %s !" %thing)
    t.sleep(2)

print ("start at %s" %t.ctime())
for i in range(2):
    th1 = th.Thread(target=do_sth, args=("home work",))
    th1.start()    #启动线程
    th1.join()     #阻塞主线程等待所有线程结束
print ("end at %s" %t.ctime())


执行结果:共计耗时就是执行两次do_sth()的时间,因为有join方法阻塞等待
# python test.py
start at Tue Apr  9 14:31:42 2019
do home work !
do home work !
end at Tue Apr  9 14:31:46 2019


稍微改一下代码

import threading as th
import time as t

def do_sth(thing):
    print ("do %s !" %thing)
    t.sleep(2)

print ("start at %s" %t.ctime())
for i in range(2):
    th1 = th.Thread(target=do_sth, args=("home work",))
    th1.setDaemon(True)
    th1.start()
print ("end at %s" %t.ctime())

执行结果:主线程一秒内完成,设置Daemon后主线程不在阻塞等待,将子线程放入后台执行,同样执行两次do_sth  这次主线程耗时不到一秒
python test.py
start at Tue Apr  9 14:38:10 2019
do home work !
do home work !
 end at Tue Apr  9 14:38:10 2019
[root@client-152 pwh]# cat test.py

再来看看将上一段代码中的th1.setDaemon(True) 注释或删除掉  执行结果如下
python test.py
start at Tue Apr  9 14:41:35 2019
do home work !
 end at Tue Apr  9 14:41:35 2019
 do home work !
这里可以看出,启动子线程后 主线程继续执行,不去管子线程执行结果

看看其它方法:

import threading as th
import time as t

def do_sth(thing):
    print ("do %s !" %thing)
    t.sleep(2)

print ("start at %s" %t.ctime())
for i in range(2):
    th1 = th.Thread(target=do_sth, args=("home work",))
    th1.setName("th1")
    th1.start()
print("the threading name is %s" %th1.getName())
print("th1 is also alive? %s" % th1.is_alive())
print ("end at %s" %t.ctime())
t.sleep(4)
print("th1 is also alive? %s" % th1.is_alive())

执行结果:
 python test.py
start at Tue Apr  9 15:10:54 2019
do home work !
 the threading name is th1
do home work !
th1 is also alive? True
end at Tue Apr  9 15:10:54 2019
th1 is also alive? False

这里有个相对比较齐全的方法,代码比较直白,没有使用技巧。不过倒是很清晰的看出来各个方法的使用过程

import threading
import multiprocessing
import time
def readf(file, sls):
    stt = time.time()
    with open(file, 'rb') as f:
        ret = f.read()
    time.sleep(sls)
    print("Read %s used %s in %s!" %(file, (time.time()-stt), threading.Thread.getName(threading.current_thread())))
    return ret

t1 = threading.Thread(target=readf, args=("art.txt", 2,))
t1.setName("readart2")
t2 = threading.Thread(target=readf, args=("test.txt", 4,))
t2.setName("readtest4")
threads = [t1, t2]

print(time.ctime())
for i in threads:
    i.start()
#    i.join()
print(time.ctime())

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值