简体   繁体   English

Python Socket服务器在后台检查连接

[英]Python Socket server check for connection in the background

I am working on a Wi-Fi thermostat with an app running on my iPhone. 我正在使用在iPhone上运行的应用开发Wi-Fi恒温器。 It uses sockets to communicate with a python program using the built in socket library. 它使用套接字使用内置的套接字库与python程序进行通信。 The problem I'm having though, is that I would like to be able to change the temperature when the phone is not connected however the server will search for 1 second then time out (minimum time for the iPhone to connect) this doesn't allow me to adjust the temperature with a rotary encoder smoothly through. 我遇到的问题是,我希望能够在未连接手机时更改温度,但是服务器将搜索1秒钟,然后超时(iPhone连接的最短时间),这不会请允许我用旋转编码器平稳地调节温度。 is there a way to listern in the background? 有没有办法在后台进行侦听?

import sys
import socket
import os
import time
temp = 15
while True:
    try:
        HOST = '192.168.1.22'
        PORT = 10000
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.bind((HOST, PORT))
        s.listen(1)
        s.settimeout(1)
        conn, addr = s.accept()
        #conn.send(str(9).encode())
        conn.send(str(temp).encode())
        while True:
            data = conn.recv(1024)
            if not data: break
            print(data)
            print(data.decode())
            data2 = data.decode()
            if int(data2) in range(5, 31):
                print(data2)
                print("Setting the temperature to " + str(data2) + "°")
                conn.send(("Setting the temperature to " + str(data2)).encode())
                temp = data2
            else:
                print("Not in range")
                conn.send("Not in range!\n".encode())
    except:
        print("No Connection!")

Thanks! 谢谢!

Your terminology is a bit confusing, but I think I see what you're trying to do. 您的术语有点混乱,但是我想我明白您正在尝试做的事情。 When your program executes accept with a 1 second timeout, that's not "searching" -- it's simply waiting for a client. 当您的程序在1秒钟的超时时间内执行accept时,这不是在“搜索”,而是在等待客户端。

It sounds like you should split your code into three pieces: 听起来您应该将代码分成三部分:

  1. actually adjusts the thermometer 实际调整温度计
  2. listens for a connection from your iPhone client (TCP listener) 侦听来自iPhone客户端的连接(TCP侦听器)
  3. waits for ("listens for") adjustments from the rotary encoder. 等待旋转编码器的(“监听”)调整。

I would put each in a separate thread (using the python threading module). 我将每个放在一个单独的线程中(使用python threading模块)。 Create a FIFO queue (with the queue module). 创建一个FIFO队列(使用queue模块)。 Have the first (thermostat-adjuster) thread wait on the queue ( Queue.get ), and have the other two accept instructions (from TCP and rotary encoder, respectively) and feed commands through the queue ( Queue.put ). 让第一个(恒温器调节器)线程在队列( Queue.get )上等待,让其他两个(分别来自TCP和旋转编码器)接受指令,并通过队列( Queue.put )提供命令。

Then you can get rid of the timeout in your TCP listener, and just have it block on the accept indefinitely. 然后,您可以摆脱TCP侦听器中的超时,而只是无限期地将其阻塞在accept Likewise, your rotary encoder listener can simply wait for adjustments. 同样,您的旋转编码器侦听器可以简单地等待调整。 And the thermostat-adjuster thread can just block waiting on instructions from the queue. 恒温器调节器线程可以阻止等待队列中的指令。 This makes all three much easier to program, understand and troubleshoot. 这使得这三个程序都易于编程,理解和故障排除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM