鱼C论坛

 找回密码
 立即注册
查看: 1931|回复: 1

Python3 主线程新开一条子线程执行while循环造成主线程阻塞

[复制链接]
发表于 2021-5-26 15:50:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import socket  # 导入 socket 模块
  2. import threading

  3. from flask import Flask
  4. from flask import render_template
  5. from flask import request
  6. import time


  7. def send_to_open():
  8.     global c

  9.     a1 = "01 0F 00 00 00 04 01 00 3E 96"
  10.     a2 = "01 05 00 02 FF 00 2D FA"
  11.     a3 = "01 05 00 03 FF 00 7C 3A"
  12.     a4 = "01 0F 00 00 00 04 01 00 3E 96"

  13.     print(bytes.fromhex(a1))
  14.     c.send(bytes.fromhex(a1))
  15.     time.sleep(0.5)

  16.     print(bytes.fromhex(a2))
  17.     c.send(bytes.fromhex(a2))
  18.     time.sleep(0.5)

  19.     print(bytes.fromhex(a3))
  20.     c.send(bytes.fromhex(a3))
  21.     time.sleep(3)

  22.     print(bytes.fromhex(a4))
  23.     c.send(bytes.fromhex(a4))


  24. def kill_socket():
  25.     global c
  26.     c.close()


  27. def connect():
  28.     s = socket.socket()  # 创建 socket 对象
  29.     host = "0.0.0.0"  # 获取本地主机名
  30.     port = 6666  # 设置端口
  31.     s.bind((host, port))  # 绑定端口
  32.     s.listen(1)  # 等待客户端连接
  33.     a, addr = s.accept()  # 建立客户端连接
  34.     return a


  35. # 按间距中的绿色按钮以运行脚本。
  36. def open_door_test():
  37.     # 全局变量
  38.     # qos:互斥锁
  39.     # c: socket连接对象
  40.     global c
  41.     global qos
  42.     if qos == 1:

  43.         qos = 0
  44.         try:
  45.             send_to_open()

  46.         except:
  47.             print('关闭了正在占线的链接!')
  48.             kill_socket()
  49.             print('关闭实例')

  50.             c = connect()
  51.             print('新建实例')

  52.             send_to_open()
  53.             print('重新开门')

  54.         qos = 1


  55. def test_connect():
  56.     global c
  57.     global qos
  58.     a1 = "01 0F 00 00 00 04 01 00 3E 96"
  59.     while True:
  60.         if qos == 1:
  61.             try:
  62.                 print(bytes.fromhex(a1))
  63.                 c.send(bytes.fromhex(a1))
  64.                 time.sleep(0.5)
  65.                 c.send(bytes.fromhex(a1))
  66.                 time.sleep(0.5)
  67.                 c.send(bytes.fromhex(a1))
  68.                 time.sleep(0.5)
  69.                 c.send(bytes.fromhex(a1))
  70.                 time.sleep(0.5)
  71.                 time.sleep(600)
  72.             except:
  73.                 print('关闭了正在占线的链接!')
  74.                 kill_socket()
  75.                 print('关闭实例')

  76.                 c = connect()
  77.                 print('新建实例')


  78. app = Flask(__name__)
  79. c = connect()
  80. qos = 1
  81. User_admin = ['qqbot', '1007800006', '2200475850', '1821269010', '1005944615', '1038888008', 'hardware']


  82. @app.route('/', methods=['GET', 'POST'])
  83. def index():
  84.     if request.args.get('user') in User_admin:
  85.         open_door_test()
  86.         return render_template('index.html')
  87.     else:
  88.         return render_template('bad.html')


  89.     app.run(port=14725, host='0.0.0.0')

  90. if __name__ == "__main__":
  91.     conn = threading.Thread(target=test_connect())
  92.     conn.start()
  93.     app.run(port=14725, host='0.0.0.0')

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-23 15:47:17 | 显示全部楼层
不要用锁就会阻塞住了,每个实例都用不同的变量名或者直接装在列表里就不会用变量名的干涉了。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 05:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表