python螺纹卡住
我正在尝试从一个称为 cameraprocessing 的线程切换到一个称为 serverkeypoints 和viceversa的线程。 确切地说, cameraprocessing 更改了一个全局变量,称为 value serverkeypoints 通过通过WebSocket发送给客户端来消耗全局值。为了保护全局变量,我使用了条件机制。
我有两个问题:
- 在某个时刻,脚本卡住了,线程不向前走。Websocket
- 客户端未接收通过WebSocket发送的数据,
这两个线程位于一个名为 main_server.py.py (我知道这不是最好的主意,最好将这些线程拆分在不同的文件中)。
main_server.py
import threading
import asyncio
import websockets
condition = threading.Condition()
VALUE = 0
FLAG = 0
class ServerKeypoints(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
async def communicate(self, websocket):
global VALUE
global FLAG
while True:
condition.acquire()
if FLAG == 1:
FLAG = 0
print(f"SERVER VAL: {VALUE}")
await websocket.send(f"{VALUE}")
condition.notify_all()
else:
condition.wait()
condition.release()
async def main(self,):
async with websockets.serve(self.communicate, "localhost", 9998):
await asyncio.Future() # run forever
def run(self):
asyncio.run(self.main())
class CameraProcessing(threading.Thread):
def __init__(self) -> None:
threading.Thread.__init__(self)
def run(self):
global VALUE
global FLAG
while True:
condition.acquire()
if FLAG == 0:
VALUE += 1
print(f"CAMERA VAL: {VALUE}")
FLAG = 1
condition.notify_all()
else:
condition.wait()
condition.release()
client.py 是按照以下方式编写的:
import websocket
def on_message(wsapp, message):
message = message
print(message)
wsapp = websocket.WebSocketApp("ws://localhost:9998", on_message=on_message)
while True:
wsapp.run_forever()
在Visual Studio Code终端中,我有这样的结果:
CAMERA VAL: 20301
SERVER VAL: 20301
CAMERA VAL: 20302
SERVER VAL: 20302
CAMERA VAL: 20303
SERVER VAL: 20303
CAMERA VAL: 20304
SERVER VAL: 20304
CAMERA VAL: 20305
SERVER VAL: 20305
CAMERA VAL: 20306
SERVER VAL: 20306
CAMERA VAL: 20307
SERVER VAL: 20307
CAMERA VAL: 20308
SERVER VAL: 20308
CAMERA VAL: 20309
SERVER VAL: 20309
CAMERA VAL: 20310
SERVER VAL: 20310
CAMERA VAL: 20311
SERVER VAL: 20311
CAMERA VAL: 20312
SERVER VAL: 20312
CAMERA VAL: 20313
SERVER VAL: 20313
但是它不会前进。
I'm trying to switch from one thread called CameraProcessing to another thread called ServerKeypoints and viceversa.
To be precise, CameraProcessing changes a global variable called VALUE while ServerKeypoints consumes the global value by sending it via websocket to a client. To protect the global variable I've used the Condition mechanism.
I have two problems:
- At a certain moment the script stucks, the threads don't go forward
- The websocket client doesn't receive the data sent via websocket
The two threads are located in a script called main_server.py (I know that it is not the best idea and that it would be better to split these threads in different files).
main_server.py
import threading
import asyncio
import websockets
condition = threading.Condition()
VALUE = 0
FLAG = 0
class ServerKeypoints(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
async def communicate(self, websocket):
global VALUE
global FLAG
while True:
condition.acquire()
if FLAG == 1:
FLAG = 0
print(f"SERVER VAL: {VALUE}")
await websocket.send(f"{VALUE}")
condition.notify_all()
else:
condition.wait()
condition.release()
async def main(self,):
async with websockets.serve(self.communicate, "localhost", 9998):
await asyncio.Future() # run forever
def run(self):
asyncio.run(self.main())
class CameraProcessing(threading.Thread):
def __init__(self) -> None:
threading.Thread.__init__(self)
def run(self):
global VALUE
global FLAG
while True:
condition.acquire()
if FLAG == 0:
VALUE += 1
print(f"CAMERA VAL: {VALUE}")
FLAG = 1
condition.notify_all()
else:
condition.wait()
condition.release()
While client.py is written in the following way:
import websocket
def on_message(wsapp, message):
message = message
print(message)
wsapp = websocket.WebSocketApp("ws://localhost:9998", on_message=on_message)
while True:
wsapp.run_forever()
In the Visual Studio Code terminal I have a result like this:
CAMERA VAL: 20301
SERVER VAL: 20301
CAMERA VAL: 20302
SERVER VAL: 20302
CAMERA VAL: 20303
SERVER VAL: 20303
CAMERA VAL: 20304
SERVER VAL: 20304
CAMERA VAL: 20305
SERVER VAL: 20305
CAMERA VAL: 20306
SERVER VAL: 20306
CAMERA VAL: 20307
SERVER VAL: 20307
CAMERA VAL: 20308
SERVER VAL: 20308
CAMERA VAL: 20309
SERVER VAL: 20309
CAMERA VAL: 20310
SERVER VAL: 20310
CAMERA VAL: 20311
SERVER VAL: 20311
CAMERA VAL: 20312
SERVER VAL: 20312
CAMERA VAL: 20313
SERVER VAL: 20313
But it does not go forward.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如Michal Butscher在评论中所建议的那样,我再次尝试了由 WebSockets提供的客户端脚本 模块,并且我已经通过以下方式进行了修改:
现在它不了t卡住。
我知道组合 async 和 thread 不是一个好主意,但是如果您有替代方案,我将很高兴阅读它们。
As suggested by Michal Butscher in the comments, I've tried again the client script provided by the websockets module and I've modified in the following way:
Now it doesn't stuck.
I know that the combination async and thread is not a good idea but if you have alternatives to propose I will be glad to read them.
好的,但是您不需要创建线程即可使用全局变量。您的
serverkeypoints
线程和您的cameraprocessing
线程永远不会同时执行任何有趣的事情,那么为什么不只是一个这样做的一个线程呢?摆脱
条件
,摆脱flag
,摆脱线程。这要简单得多,而且(我很确定)它将执行与您的示例代码相同的事情。OK, but you don't need to create threads in order to use global variables. Your
ServerKeypoints
thread and yourCameraProcessing
thread never do any interesting thing concurrently with each other, so why not just have one thread that does this?Get rid of the
condition
, get rid of theFLAG
, get rid of the threads. It's a whole lot simpler, and (I'm pretty sure) it will do the same as what your example code does.