惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
GbyAI
GbyAI
Engineering at Meta
Engineering at Meta
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
H
Hackread – Cybersecurity News, Data Breaches, AI and More
WordPress大学
WordPress大学
博客园_首页
美团技术团队
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
宝玉的分享
宝玉的分享
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
J
Java Code Geeks
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog
雷峰网
雷峰网
爱范儿
爱范儿

博客园 - 哈哈

cas服务器搭建后的调整 使用python直接连接http服务器获取数据 request的url地址 numpy的多项式函数与求导 pandas相关 tcp与ip的包结构 江苏铁路 易经-64卦 使用python实现rpc服务 图书分类-中图法,值得一看的图书 linux时区,curl的使用,nginx日志,watch 使用selenium模仿登录,执行js python与java的交互 计算斐波那契数列 selenium的使用 pip使用 python读取postgresql 18位身份证的验证码 python读取docx文件属性-最后一次保存的时间等
Python线程
哈哈 · 2021-03-03 · via 博客园 - 哈哈
import gevent

def _task(timeout, pid, msg):
    print("I'm task %d and want to say: %s" % (pid, msg))
    gevent.sleep(timeout)
    print("Task %d done." % (pid,))

def test():
    g0 = gevent.spawn(_task, 1, 0, 'just 0 test')
    g1 = gevent.spawn(_task, 2, 1, 'just 1 test')
    g2 = gevent.spawn(_task, 4, 2, 'just 2 test')
    gevent.joinall([g0, g1, g2])
    print('run end')

test()


from greenlet import greenlet
import time

def test1(gr,g):
    for i in range(10):
        print("---A--:" + str(i))
        gr.switch(g, gr) # 切换到另一个协程执行
        time.sleep(0.5)

def test2(gr, g):
    for i in range(10):
        print("---B--" + str(i))
        gr.switch(g, gr)
        # gr.throw(AttributeError)
        time.sleep(0.5)

if __name__ == '__main__':
    # 创建一个协程1
    gr1 = greenlet(test1)
    # 创建一个协程2
    gr2 = greenlet(test2)
    # 启动协程
    gr1.switch(gr2, gr1)


from greenlet import greenlet
def test1(x,y):
    print('begin test1:')
    z = gr2.switch(x+y)
    print('test1:z:' + str(z))
def test2(u):
    print('begin test2')
    print('test2:u:' + str(u))
    gr1.switch(42)
gr1 = greenlet(test1)
gr2 = greenlet(test2)
gr1.switch("hello",'world')
import time
from threading import Thread,RLock,Condition,current_thread

def func1(c):
    c.acquire(False) #固定格式
    # print(1111)

    c.wait()  #等待通知,
    #time.sleep(3)  #通知完成后大家是串行执行的,这也看出了锁的机制了
    print('%s执行了'%(current_thread().getName()))

    c.release()

if __name__ == '__main__':
    c = Condition()
    for i in range(5):
        t = Thread(target=func1,args=(c,))
        t.start()

    while True:
        num = int(input('请输入你要通知的线程个数:'))
        c.acquire() #固定格式
        #c.notify(num)  #通知num个线程别等待了,去执行吧
        c.notifyAll()
        #c.notify_all()
        c.release()