python3之多线程及线程锁的基础用法

所需积分/C币:32 2019-01-10 18:31:48 411KB PDF
收藏 收藏 1
举报

《python3之多线程及线程锁的基础用法》线程的基本用法
print(str( threading currentThread0)+主线程运行耗时:'+str( startTime- endTime) 会立即返回目前运行的耗时。 thread join 这个joi方法会使主线程堵塞,直到 thread中的任务执行结束,才会执行主线程接下来的代码。 执行结果 C: \Users\DT283\App Data\Programs\Python(Python36python exe E: /pyworlspace/sc lainThread( Mainthread, started15688)):主程运行耗时:-0.001000811950683594 <Thread(Thread-1, started 10932)>: worl coding lainthread( fainter eac, started15689)>:主线程运行耗时:-5.00081352005005 <Thread(Thread-2, st arted 18408)>: Fo1l B: testing lainthread( ainthr ead, started1568})>:主线程运行耗时:-10.001432180404663 Pracess finished with exit code o 共号/错东 执行结果 通过执行结果我们能够看出 workA由线程 Thread-1在执行, Work B由线程 Thread-2在执行 Main thread共运行了10s 多线程实现2 出了以上的方法执行线程以外,更推荐通过继承 threading. Thread类的方式来实现线程。 main2. py mport threading import time class Thread(threading Thread): def init ( threading. Thread. init (self) seLf. job job def run(self) time.sleep(5) print(str(threading currentThread )t: workA: '+str(seLf. job)) cLass ThreadS(threading Thread): def in主t threading, Thread, init(self) self. job e job def run(self): time.s⊥eep(10 print(str(threading, currentThread ())t': wor kB:+str(self. job)) fat startTime time. time ( i #线程运行 workA thread= Thread(coding) thread. start( thread= ThreadS(testing thread. start 主线程并表等待子线程 endTime= time. time o: print(str( threading, currentthread()):主线程运行耗时:'str( startTime- endtime)) 等待 thread结束 thread join () endTime= time. time () print(str( threading, currentthread())+:主线程运行耗时:'+str( starttime endtime)) thread join() endTime= time, timeO; print(str( threading currentThread()+':主线程运行耗时:'+str( startTime. endTime) 头条号/桂堂东 main2. py 这里需要注意的是,如果子类重写了int方法的情况下,一定要调用父类 threading Thread的 init方法。 ■执行结果 C: \Users\DI283\AppDatalLocal\Programs\Python\Python36python exe E: /p workspace/scrap llainThread (ainThread, started13564)冫主线程运行耗时;-.000987354278564453 <ThreadA( Thre ad-1, started 11592)>: worlA: coding lainThread (lainThread, started13564)>主线程运行耗时:-5.0014070733643 <ThreadB(Thre ad-2, started 3480)2: worLB: testing llainThre ad ( Thread, started13564)>:主线程运行耗时:-10.00177550315857 Process finished with esit code o 头济号/ 执行结果 线程锁 我们在使用多线程进行操作的时候,很多时候会用到线程锁 比如上面的例子中,WokA和 workB是互斥的。当 workA运行的过程中,WokB不能运行。反之亦 然 那么在这种情况下,就需要用到线程锁。 main py port threading mport time 设置线程锁 lock threading Lock( class Thread(threading Thread): def- init (self, job): nit threading, Thread ,init(self) f- job def run(seLf): lock, acquired) time.sleep(5) print(str(threading currentThread()): workA: 'str(self. job) lock release() class ThreadS( threading. Thread): def init threading Thread. init(seLf) seLf. jobjob def run(seLf) lock,acquire() time. sleep(10) print(str(threading, currentThread ())+' workB: ' str(self. job)) startTime time. time ( i 线程运行 workA thread Thread( coding) thread. start( #线程运行 workB thread ThreadS( testing) thread. start #主线程并未等待子线程 endTime= time. time(; print(str( threading. currentThread())+":主线程运行耗时:'+str( starttime- endTime) 等特 threads结变 thread, join() end Time s time. timed print(str( threading. current thread())+":主线程运行耗时:'tstr( startTime- endTime) thread join( endTime time. time(; print(str( threading. currentThread()+"!主线程远行耗时:‘+str( startTime- endTime)) 头条号/桂堂东 mains. py 我们通过 lock= threading LockO 设置一个线程锁。 oCK.acquire 请求获取锁 lock. released 释放锁 执行结果 C: \Users\ DI28\App Data Local\Programs\PythonPython36\python ese E: /pyworkspaee/scrap Spi < lLain Thre ad lainThread, started10040)>主线程运行耗时:-0.00996891021728516 <Thre adA(Thread-1, started 16943)>: worl:A: coding Iain Thre ad clain thread, started10040)>主线程运行耗时:-5.001620531082153 <Thread (Thread-2, started 17756)2: orl B testing Iain thre ad gain thread, started1040)>主线程运行耗时:-15.00192332677612 Process finished ith exit code o 执行结果 最终主线程的运行时间为155。等于WokA的55加上WokB的10

...展开详情
试读 7P python3之多线程及线程锁的基础用法
立即下载 低至0.43元/次 身份认证VIP会员低至7折
抢沙发
一个资源只可评论一次,评论内容不能少于5个字
关注 私信 TA的资源
上传资源赚积分,得勋章
最新推荐
python3之多线程及线程锁的基础用法 32积分/C币 立即下载
1/7
python3之多线程及线程锁的基础用法第1页
python3之多线程及线程锁的基础用法第2页

试读结束, 可继续读1页

32积分/C币 立即下载 >