python socket thread

import socket
from urllib.parse import urlparse

def get_url(url):
    url=urlparse(url)
    host=url.netloc
    path=url.path

    if not path:
        path="/"
    print(host, path)
    client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    client.connect((host,80))
    client.send("'GET {} HTTP/1.1\r\nHost: {}:{}\r\nConnection: close\r\n\r\n'".format(path,host,80).encode('utf8'))
    data=b""
    while True:
        d=client.recv(1024)
        if d:
            data+=d
        else:
            break
    print(data.decode('utf8'))
    client.close()
import time,threading
def get_url():
    print("start get url")
    time.sleep(2)
    print("get url end")

def get_url_detail():
    print("start get url detail")
    time.sleep(4)
    print("get url detail end")

class getUrl(threading.Thread):
    def __init__(self,name):
        super.__init__(name=name)
    def run(self):
        print("start get url detail")
        time.sleep(4)
        print("get url detail end")

if __name__=="__main__":
    tim=time.time()
    thread1=threading.Thread(target=get_url)
    thread2 = threading.Thread(target=get_url_detail)##getUrl("getDeatil")
    #thread2.setDaemon(True)##当main主线程结束后 不会待执行完成就将杀掉该线程
    thread1.start()
    thread2.start()
    thread1.join()##join 等待该线程执行完后再执行其它的线程 所有最后的时间差是最大的时间
    thread2.join()
    print("time:"+str(time.time()-tim))

线程间通信queue

from queue import Queue
import time,threading
def get_url(url_queue):
    while True:
        print("start get url")
        #time.sleep(2)
        for a in range(100):
            url_queue.put(a)
        print("get url end")


def get_url_detail(url_queue):
    while True:
        url=url_queue.get()
        print("start get url detail:%s"%url)
        #time.sleep(4)
        print("get url detail end")

if __name__=="__main__":
    url_queue=Queue(maxsize=1000)
    thread1=threading.Thread(target=get_url,args=(url_queue,))
    thread2 = threading.Thread(target=get_url_detail, args=(url_queue,))
    thread1.start()
    thread2.start()

 线程锁lock condition

##用lock会影响性能 并会引起死锁 同一个线程里lock acquire与release必须配对 一样多
##用Condition notify wait必须在with condition上下文内使用 否则也可以notify wait前用condition.acquire()线束后condition.release()
import threading
from threading import Lock,Condition
a=0
def add(lock):
    global  a
    for i in range(1000):
        lock.acquire()
        ##lock.acquire()此处就会引起死锁 还有就是两个线程用了两个相同的锁
        a=a+1
        lock.release()
def desc(lock):
    global a
    for i in range(1000):
        lock.acquire()
        a=a-1
        lock.release()

class XiaoAiRobot(threading.Thread):
    def __init__(self,cond):
        self.cond=cond
        super().__init__(name="xiaoai")
    def run(self) -> None:
        with self.cond:
            print("xiaoai>>:%s"%"你好,天猫")
            self.cond.notify()
            self.cond.wait()

            print("xiaoai>>:%s"%"我们来对诗吧")
            self.cond.notify()
            self.cond.wait()

            print("xiaoai>>:%s"%"床前明月光")
            self.cond.notify()
            self.cond.wait()

            print("xiaoai>>:%s"%"举头望明月")
            self.cond.notify()
            self.cond.wait()
class TmallRobot(threading.Thread):
    def __init__(self,cond):
        self.cond = cond
        super().__init__(name="tamll")
    def run(self) -> None:
        with self.cond:
            self.cond.wait()
            print("tmall>>:%s"%"你好,小爱")
            self.cond.notify()

            self.cond.wait()
            print("tmall>>:%s"%"好吧")
            self.cond.notify()

            self.cond.wait()
            print("tmall>>:%s"%"疑是地上霜")
            self.cond.notify()

            self.cond.wait()
            print("tmall>>:%s"%"低头思故乡")
            self.cond.notify()
if __name__=="__main__":
    # lock=Lock()
    # lockthred=threading.Thread(target=add,args=(lock,))
    # lockthred1 = threading.Thread(target=desc, args=(lock,))
    # lockthred.start()
    # lockthred1.start()
    # print(a)##0
    cond=Condition()

    condthread1=XiaoAiRobot(cond)
    condthread2 = TmallRobot(cond)
    condthread2.start()
    condthread1.start()
xiaoai>>:你好,天猫
tmall>>:你好,小爱
xiaoai>>:我们来对诗吧
tmall>>:好吧
xiaoai>>:床前明月光
tmall>>:疑是地上霜
xiaoai>>:举头望明月
tmall>>:低头思故乡

 

posted @ 2019-11-30 10:21  howhy  阅读(298)  评论(0)    收藏  举报