当前位置: 首页 > news >正文

做企鹅号的视频素材网站网络企业推广

做企鹅号的视频素材网站,网络企业推广,怎么建立微信网站,最新的销售平台需求:在Python-主线程控制子线程-3的基础上,新增使用UDP接收指令功能,代替从键盘输入指令 # 修改后的程序,主线程可以获取子线程的结果 import threading import time import queue import tracebackfrom loguru import logger i…

需求:在Python-主线程控制子线程-3的基础上,新增使用UDP接收指令功能,代替从键盘输入指令

# 修改后的程序,主线程可以获取子线程的结果
import threading
import time
import queue
import tracebackfrom loguru import logger
import socketclass WorkerThread(threading.Thread):def __init__(self, result_queue):super().__init__()self.stop_event = threading.Event()self.result_queue = result_queuestatic_variable = 0def run(self):thread_id = threading.get_ident()print(f"Worker thread {thread_id} has started.")while not self.stop_event.is_set():print(f"Worker thread {thread_id} is running.")result = self.do_work()self.result_queue.put(result)time.sleep(1)print(f"Worker thread {thread_id} has stopped.")def increment_static_variable(cls):cls.static_variable += 1def do_work(self):  # Simulate some work# self.increment_static_variable()self.static_variable += 1return self.static_variabledef stop(self):self.stop_event.set()self.join()  # 等待子线程结束class KeyListener:def __init__(self, result_queue_listener):self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)self.address = ("127.0.0.1", 12580)self.server_socket.bind(self.address)self.stop_event = threading.Event()self.result_queue_listener = result_queue_listenerdef udp_recv(self):while not self.stop_event.is_set():try:receive_dat, client_address = self.server_socket.recvfrom(1024)command = receive_dat.decode()print(command)self.result_queue_listener.put(command)except socket.error:# This will be triggered when the socket is closedpassdef start(self):keyboard_thread = threading.Thread(target=self.udp_recv)  # 创建键盘输入监听线程keyboard_thread.start()logger.info(f"Keyboard monitor thread {keyboard_thread.ident} has started~")def stop(self):self.stop_event.set()self.server_socket.close()def end_child_thread():try:if worker_thread.is_alive():worker_thread.stop()logger.info(f"Stopping worker thread {worker_thread.ident}...")if worker_thread.is_alive():logger.info(f"Worker thread {worker_thread.ident} is still active")else:logger.info(f"Worker thread {worker_thread.ident} doesn't exist ..")except Exception as e:logger.info("中止线程失败:" + e)if __name__ == '__main__':result_queue = queue.Queue()result_queue_listener = queue.Queue()main_thread_id = threading.main_thread().identlogger.info(f"Main thread {main_thread_id} has started~")worker_thread = WorkerThread(result_queue)worker_thread.start()logger.info(f"worker thread {worker_thread.ident} has started~")key_listener = KeyListener(result_queue_listener)key_listener.start()exit_program = Falsetry:while not exit_program:if not result_queue_listener.empty():command = result_queue_listener.get()if command == '0':logger.info(f"工作线程状态{worker_thread}")end_child_thread()logger.info(f"工作线程状态{worker_thread}")elif command == '1':try:if worker_thread.is_alive():passelse:worker_thread = WorkerThread(result_queue)worker_thread.start()logger.info(f"Started new worker thread {worker_thread.ident}")except Exception as e:logger.info("新建工作线程失败:" + e)elif command == '2':end_child_thread()key_listener.stop()print(f"停止工作线程{worker_thread}")print(f"停止监听线程{key_listener}")exit_program = Truebreakelse:passtime.sleep(0.1)if not result_queue.empty():result = result_queue.get()print("主线程获取的子线程结果:", result)else:passexcept Exception as e:print("Exception caught:", e)traceback.print_exc()  # 打印详细的异常堆栈信息end_child_thread()key_listener.stop()print(f"except工作线程{worker_thread}")print(f"except监听线程{key_listener}")# 主线程退出logger.info(f"Main thread {main_thread_id} is exiting~")

使用网络调试助手向程序发送指令,运行效果如下:

 

http://www.hkea.cn/news/983309/

相关文章:

  • 做购物网站需不需要交税费郑州网站托管
  • 是不是做网站就能赚钱谷歌seo关键词优化
  • 萝岗门户网站建设今日重大新闻头条财经
  • 个人相册网站模板怎么把网站排名排上去
  • 建设外贸网站案例统计站老站长推荐草莓
  • 1688网站的特点全网营销系统
  • 西安做网站企业网址怎么申请
  • 专业网站建设品牌百度指数里的资讯指数是什么
  • 做网站规避什么网站制作
  • 网站开发工作方案2022拉人头最暴利的app
  • 网站建设好还是阿里巴巴好磁力猫torrent kitty
  • wordpress跨域访问前端优化网站
  • 住房和城乡建设部网站买卖合同北京中文seo
  • WordPress自己安装了插件深圳seo推广外包
  • 建立网站舆情分析报告范文
  • wordpress 空格 插件班级优化大师app
  • 成都有实力的网站建设网络培训心得
  • 中企高呈建设网站在百度怎么创建自己的网站
  • 女的和女的做那个视频网站怎么在网上做网络营销
  • 网站开发需要什么软件百度怎样发布作品
  • 专门做宠物食品的网站市场调研怎么做
  • 兰州网站建设q.479185700棒成年s8视频加密线路
  • 付费网站推广seo关键词排名优化怎么收费
  • 网站由那些组成google网页搜索
  • 对一个网站做性能测试谷歌paypal官网入口
  • 北京住房投资建设中心网站首页快速排名怎么做
  • 中国网站制作 第一个佛山网站优化
  • thinkphp做的教育网站微商引流推广
  • 做特卖网站手机版电商最好卖的十大产品
  • 怎样做网站平叿trinseo公司