python线程等待模板

自己写的一个比较好用的线程等待代码模板,python3环境下
我的应用场景:在拨号爬虫的时候,重新拨号之后数据库的连接会出现异常,所以需要用到
简介:A是爬虫线程,B是写控制爬虫线程什么时候停止,然后运行一些其他脚本的线程代码中主要运行逻辑我用的sleep代替


import random
import time
import threading
class A(threading.Thread):
    def __init__(self,thread_id,event):
        super(A, self).__init__()
        self.thread_id=thread_id
        self.event=event
        self.stopped=False
    def run(self):
        while 1:
            if self.event.is_set():
                sleep_time=random.randrange(5,10)
                print(self.thread_id,'准备等待:',sleep_time)
                time.sleep(sleep_time)
                print(self.thread_id,"等待完毕")
            else:
                self.stopped=True
                print(self.thread_id,"线程阻塞,等待中!")
                time.sleep(1)
class B(threading.Thread):
    def __init__(self,thread_pool):
        super(B, self).__init__()
        self.thread_pool=thread_p
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值