V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  zy820  ›  全部回复第 1 页 / 共 2 页
回复总数  22
1  2  
2018-08-03 10:57:35 +08:00
回复了 zy820 创建的主题 Python socketserver.server_forever()+多线程处理数据并存入数据库
if __name__ == "__main__":
sensor_que = queue.Queue()
sensor_data = {'DeviceId': '', 'AirPressure': 0, 'Humidity': 0, 'Noise': 0, 'Pm25': 0, 'Temperature': 0,
'WindDirection': 0, 'WindSpeed': 0}
# HOST, PORT = "localhost", 9999 #windows
HOST, PORT = "0.0.0.0", 9999 # Linux
server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) # 线程
server.serve_forever()
print('before init_db!')
connect_sql()
init_db()
print('after init_db!')
for i in range(multiprocessing.cpu_count()):
t = threading.Thread(target=getsensor_que)
t.start()
2018-08-02 13:39:06 +08:00
回复了 zy820 创建的主题 问与答 socket server 接收数据,用 queue+sql 多线程处理问题
没有大神指点一下吗
2018-07-26 22:14:01 +08:00
回复了 zy820 创建的主题 问与答 socket server 接收数据,用 queue+sql 多线程处理问题
def getsensor_que():
while True:
sensor = sensor_que.get(block=True) #阻塞,queue 为空时,不会 Queue.Empty 异常
print(sensor)
savetosql(sensor)

for i in range(multiprocessing.cpu_count()):
t = threading.Thread(target=getsensor_que)
t.start()
@msg7086 谢谢提醒,我的,目前根据六楼建议已解决
@copie 不是 websocket 吧,就是 tcp server 端,目前已经接收数据了,下一步存储到数据库
@msg7086 \x00\x00\x03\xe9 四个字节是一个 int 啊,现在接收要把它转化过来
@misaka19000 我觉得也是正在尝试
@imn1 [0]返回的是 int 啊
@GeruzoniAnsasu data=struct.unpack(">ii",b"\x00\x00\x00\x1e")[0] 加个 i 就行了,现在还是保存字节流并提取想要位置的字节问题
@GeruzoniAnsasu data=struct.unpack(">i",b"\x00\x00\x00\x1e")[0]只能有四个字节,现在问题时怎么把 b''字节流保存并截取我想要的字节呢
2018-07-23 21:13:35 +08:00
回复了 zy820 创建的主题 问与答 Python socketserver 写个 TCP server 接收数据
@di94sh pyrhon3,放到 socket 前要转成二进制什么意思,我用 GPRS 模块发送的字节流
2018-07-23 20:33:37 +08:00
回复了 zy820 创建的主题 问与答 Python socketserver 写个 TCP server 接收数据
unicodedecodeerror:'utf-8'Can't Decode Byte 0xe9
@mhycy 可以了,加个 if not self.data:,谢了
@mhycy client 关闭就一直死循环了
@mhycy 一直
print("{} wrote:".format(self.client_address[0]))
print(self.data)
@mhycy
import socketserver

class MyTCPHandler(socketserver.BaseRequestHandler):

def handle(self): #所有请求的交互都是在 handle 里执行的,
while True:
try:
self.data = self.request.recv(1024).strip()#每一个请求都会实例化 MyTCPHandler(socketserver.BaseRequestHandler):
print("{} wrote:".format(self.client_address[0]))
print(self.data)
#self.request.sendall(self.data.upper())#sendall 是重复调用 send.
except ConnectionResetError as e:
print("err ",e)
break

if __name__ == "__main__":
HOST, PORT = "localhost", 9999 #windows
#HOST, PORT = "0.0.0.0", 9999 #Linux
server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) #线程
server.serve_forever()
@u14e 有啥想法了吗,交流一下?
@twor TCP Server 用 flask-socketio 吗,有这样的例子吗
@strict 就是不确定 tcp 这块用什么写啊
@strict GPRS 模块上传的数据
@today4king 有例子可以学习的吗,谢谢
1  2  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2848 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 18ms · UTC 15:21 · PVG 23:21 · LAX 08:21 · JFK 11:21
Developed with CodeLauncher
♥ Do have faith in what you're doing.