socket网络编程多线程
生活随笔
收集整理的這篇文章主要介紹了
socket网络编程多线程
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#!/usr/bin/env python
#-*-coding:utf-8-*-
#多進(jìn)程
import threading
import time
def thfun():s=0for i in range(30):s+=itime.sleep(0.1)print(s)class MyThread(threading.Thread):def run(self):s=0for i in range(30):s+=itime.sleep(0.1)print(s)if __name__=='__main__':#ths=[threading.Thread(target=thfun) for i in range(2)]ths=[MyThread() for i in range(2)]for th in ths:th.start()#!/usr/bin/env python
#-*-coding:utf-8-*-
#線程間等待 join 線程等待
import threading,timeclass MyThread(threading.Thread):def run(self):for i in range(30):print('threading:',i)time.sleep(0.1)if __name__=='__main__':t=MyThread()t.start()#制定超時(shí)1s#t.join(1)t.join()for i in range(10):print('Main:',i)time.sleep(0.1)
#!/usr/bin/env python
#-*-coding:utf-8-*-
#后臺(tái)線程 daemon
#后臺(tái)線程不會(huì)結(jié)束import threading,timedef dmn():print('dmn start...')time.sleep(2)print('dmn end.')def ndmn():print('ndmn start...')time.sleep(1)print('ndmn end.')d=threading.Thread(target=dmn)
#后臺(tái)線程
d.daemon=True
n=threading.Thread(target=ndmn)print('start...')
d.start()
n.start()
print('end...')#!/usr/bin/env python
#-*-coding:utf-8-*-
#線程等待,指令鎖 可重入鎖,acquire(blocking=True,timeout=-1) release() threading.RLock
import threading,time,randomshare=4class MyThread(threading.Thread):def __init__(self,i):super().__init__()self.i=idef run(self):global share for d in range(3):lock.acquire()print(share)share+=self.itime.sleep(random.random())print('+',self.i,'=',share)lock.release()lock=threading.Lock()if __name__=='__main__':t=MyThread(2)tt=MyThread(6)t.start()tt.start()#!/usr/bin/env python
#-*-coding:utf-8-*-
#條件變量 生產(chǎn)者,消費(fèi)者
import threading,timeshare=0share_cond=threading.Condition()class ProThread(threading.Thread):def __init__(self):super().__init__()self.name='Produce'def run(self):global shareif share_cond.acquire():while True:if not share:share+=1print(self.name,share)#喚醒需要本資源的線程share_cond.notify()share_cond.wait()time.sleep(1)#消費(fèi)者
class CustomThread(threading.Thread):def __init__(self):super().__init__()self.name="Custom"def run(self):global shareif share_cond.acquire():while True:if share:share-=1print(self.name,share)share_cond.notify()share_cond.wait()time.sleep(1)
if __name__=='__main__':t=ProThread()tt=CustomThread()t.start()tt.start()#!/usr/bin/env python
#-*-coding:utf-8-*-
#線程同步,信號(hào)量
import threading, timesema=threading.Semaphore(2)
#資源的個(gè)數(shù)class MyThread(threading.Thread):def __init__(self,name):super().__init__()self.name=namedef run(self):#獲得一個(gè)資源if sema.acquire():print(self.name,'Had got resource.')time.sleep(1)sema.release()print(self.name,'Had released resource.')
if __name__=='__main__':ths=[MyThread(str(i)+'Sema') for i in range(5)]for th in ths:th.start()
#!/usr/bin/env python
#-*-coding:utf-8-*-
#等待線程 ,線程通信
#定時(shí)執(zhí)行 threading.Timer threading.Timer(3,start)import threading
import timeevent=threading.Event()#等待線程
class MyThreadWait(threading.Thread):def run(self):self.name='Wait Thread'print(self.name,'Wait...')event.wait()#等到內(nèi)部標(biāo)記為Trueprint(self.name,'Start...')event.clear()class MyThreadMain(threading.Thread):def run(self):time.sleep(3)print('Main thread set event flag!')event.set()#內(nèi)部標(biāo)記為空if __name__=='__main__':thw=MyThreadWait()thm=MyThreadMain()thw.start()thm.start()
總結(jié)
以上是生活随笔為你收集整理的socket网络编程多线程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 分析模板方法设计模式
- 下一篇: C语言中文件的读取和写入