同步异步,死锁,互斥锁,网络编程

“”"----------------同时进行------------------"""
from threading import Thread,Lock
import time
g=0
def hs1():
global g
for i in range(100000):
l.acquire()
g+=1
l.release()
print(“函数1:”, g)
def hs2():
global g
for i in range(100000):
l.acquire()
g += 1
l.release()
print(“函数2:”,g)
if name == ‘main’:
l=Lock()
t1=Thread(target=hs1)
t2=Thread(target=hs2)
t1.start()
t2.start()
“”"------------------不同步,不同时进行----------------"""
from threading import Thread,Lock
import time
g=0
def hs1():
global g
l.acquire() # 走的时候等另一个释放,先循环完,下一个再循环
for i in range(100000):
g+=1
print(“函数1:”, g)
l.release()
def hs2():
global g
l.acquire()
for i in range(100000):
g += 1
print(“函数2:”,g)
l.release()
if name == ‘main’:
l=Lock()
t1=Thread(target=hs1)
t2=Thread(target=hs2)
t1.start()
t2.start()
“”"------------------死锁-------------------"""
from threading import Thread,Lock
import time
g=0
def hs1():
global g
for i in range(100000):
lock1.acquire()
print(“函数1锁1得到锁,请求锁2”)
time.sleep(1)
lock2.acquire()
print(“11111111111111”)
g+=1
lock2.release()
lock1.release()
print(“函数1:”, g)

def hs2():
global g
for i in range(100000):
lock2.acquire()
print(“函数2锁2得到锁,请求锁1”)
time.sleep(1)
lock1.acquire()
print(“2222222222222”)
g += 1
lock1.release()
lock2.release()
print(“函数2:”,g)
if name == ‘main’:
lock1=Lock()
lock2=Lock()
t1=Thread(target=hs1)
t2=Thread(target=hs2)
t1.start()
t2.start()
“”"----------------能进行下去----------------"""
from threading import Thread, Lock
import time

g = 0

def hs1():
global g
for i in range(100000):
lock1.acquire()
print(“函数1锁1得到锁,请求锁2”)
time.sleep(1)
lock2.acquire()
print(“11111111111111”)
g += 1
lock2.release()
lock1.release()
print(“函数1:”, g)

def hs2():
global g
for i in range(100000):
lock1.acquire()
print(“函数2锁2得到锁,请求锁1”)
time.sleep(1)
lock2.acquire()
print(“2222222222222”)
g += 1
lock2.release()
lock1.release()
print(“函数2:”, g)

if name == ‘main’:
lock1 = Lock()
lock2 = Lock()
t1 = Thread(target=hs1)
t2 = Thread(target=hs2)
t1.start()
t2.start()
“”"-------------队列queue---------------"""
import queue
q=queue.Queue(3)
q.put(1)
q.put(2)
q.put(3)
print(“11111111111”,q.full())
q.put(4)
print(“结束”)

q=queue.Queue(3)
q.put(1)
q.put(2)
q.put(3)
while q.qsize()>0:
print(q.get())
for i in range(q.qsize()):
print(q.get())
“”"-------------队列优先级---------------"""
数字小的优先级高
import queue

q=queue.PriorityQueue(5)
q.put((1,“香蕉”))
q.put((5,“葡萄”))
q.put((2,“西瓜”))
q.put((-9,“芒果”))
while q.qsize()>0:
print(q.get())
“”"-------------后入先出队列----------------"""
import queue

q=queue.LifoQueue() # 括号不写数字无限制
q.put(1)
q.put(2)
q.put(5)
q.put(4)
while q.qsize()>0:
print(q.get())
“”"-------------生产者消费者模式-------------"""
from threading import Thread
import queue,time
def produce(q,name):
count=1
while True:
bz=name+“生产的包子%d”%count
q.put(bz)
print(bz)
time.sleep(1)
count+=1
def consum(q,name):
while True:
bz=q.get()
print(name,“吃”,bz)
if name == ‘main’:
q=queue.Queue(5)
t1=Thread(target=produce,args=(q,“张三”))
t2=Thread(target=consum,args=(q,“李四”))
t1.start()
t2.start()
“”"------------两个人包三个人吃--------------"""
from threading import Thread
import queue,time
def produce(q,name):
count=1
while True:
bz=name+“生产的包子%d”%count
q.put(bz)
print(bz)
time.sleep(1)
count+=1
def consum(q,name):
while True:
bz=q.get()
print(name,“吃”,bz)
if name == ‘main’:
q=queue.Queue(5)
t1=Thread(target=produce,args=(q,“张1”))
t3=Thread(target=produce,args=(q,“张2”))
t2=Thread(target=consum,args=(q,“赵1”))
t4=Thread(target=consum,args=(q,“赵2”))
t5=Thread(target=consum,args=(q,“赵3”))
t1.start()
t2.start()
t3.start()
t4.start()
t5.start()
“”"------------threadLocal------------------"""
from threading import Thread
import threading,time
class A():
pass
def hs1(name):
A.name=name
for i in range(1000):
pass
hs2()
def hs2():
# time.sleep(1)
print(threading.current_thread(),A.name)
if name == ‘main’:
t1=Thread(target=hs1,args=(“张三”,),name=“线程帅哥”)
t2=Thread(target=hs1,args=(“李四”,),name=“线程美女”) # 当是两个都来赋值,后面的把前面的覆盖了
一个张三一个李四是因为前一个输出完了,后一个接着输出
t1.start()
t2.start()

from threading import Thread
import threading,time
local=threading.local()
def hs1(name):
local.name=name
for i in range(1000):
pass
hs2()
def hs2():
# time.sleep(1)
print(threading.current_thread(),local.name)
if name == ‘main’:
t1=Thread(target=hs1,args=(“张三”,),name=“线程帅哥”)
t2=Thread(target=hs1,args=(“李四”,),name=“线程美女”) #各个线程间独立,互不干扰
t1.start()
t2.start()

“”"------------------------------------------"""
import threading,time

def hanshu():
s=time.time()
count=0
for i in range(200000000):
count+=1
e=time.time()
print(e-s)
if name == ‘main’:
t1=threading.Thread(target=hanshu)
t1.start()

import threading,time

def hanshu2():
s=time.time()
count=0
for i in range(100000000):
count+=1
e=time.time()
print(e-s)
if name == ‘main’:
t1=threading.Thread(target=hanshu2)
t2=threading.Thread(target=hanshu2)
t1.start()
t2.start()
“”"----------------yield--------------------"""
def hanshu():
a=3
for x in range(10):
yield x+a
a+=1

x=hanshu()
for y in x:
print(y)
“”"---------------协程-----------------"""
def hanshu():
for i in range(3):
print(“函数1”,i)
yield

def hanshu2(a):
for i in range(3):
next(a)
print(“函数2”,i)
h=hanshu()
hanshu2(h)
“”"--------------UDP聊天---------------"""
import socket
from socket import *
udp_s=socket(AF_INET,SOCK_DGRAM)
dest_address=(“10.10.107.44”,8899)
udp_s.bind((“10.10.107.44”,9999))
while True:
data=input(“请输入要发送的数据”)
udp_s.sendto(data.encode(“utf-8”),dest_address)
s=udp_s.recvfrom(124)
print(s[0].decode(“utf-8”))
即时聊天
from threading import Thread
from socket import *
udp_s=socket(AF_INET,SOCK_DGRAM)
dest_address=(“10.10.107.56”,9999)
local_address=(“10.10.107.141”,9999)
udp_s.bind(local_address)

def send():
while True:
d=input(“请输入”)
udp_s.sendto(d.encode(“utf-8”), dest_address)
def receive():
while True:
d=udp_s.recvfrom(124)
print(d[0].decode(“utf-8”))
# print(d)
if name == ‘main’:
t1=Thread(target=send)
t2=Thread(target=receive)
t1.start()
t2.start()

你可能感兴趣的:(同步异步,死锁,互斥锁,网络编程)