Antonio Cheong commited on
Commit
17e522d
·
1 Parent(s): 9ce9327

Prevent concurrent recv

Browse files
Files changed (1) hide show
  1. src/EdgeGPT.py +14 -4
src/EdgeGPT.py CHANGED
@@ -143,6 +143,9 @@ class ChatHub:
143
  self.loop: bool
144
  self.task: asyncio.Task
145
 
 
 
 
146
  async def init(self, conversation: Conversation) -> None:
147
  """
148
  Separate initialization to allow async
@@ -166,15 +169,21 @@ class ChatHub:
166
  # Construct a ChatHub request
167
  self.request.update(prompt=prompt)
168
  # Send request
 
169
  await self.wss.send(append_identifier(self.request.struct))
170
  while True:
 
 
 
171
  objects = str(await self.wss.recv()).split("")
172
  for obj in objects:
173
  if obj is None or obj == "":
174
  continue
175
  response = json.loads(obj)
176
  if response.get("type") == 2:
 
177
  return response
 
178
 
179
  async def __initial_handshake(self):
180
  await self.wss.send(append_identifier({"protocol": "json", "version": 1}))
@@ -184,12 +193,13 @@ class ChatHub:
184
 
185
  async def __ping(self):
186
  while self.loop:
187
- try:
188
- await asyncio.sleep(5)
189
- except asyncio.CancelledError:
190
- break
191
  await self.wss.send(append_identifier({"type": 6}))
192
  await self.wss.recv()
 
193
 
194
  async def close(self):
195
  """
 
143
  self.loop: bool
144
  self.task: asyncio.Task
145
 
146
+ self.pause = False
147
+ self.pause_ping = False
148
+
149
  async def init(self, conversation: Conversation) -> None:
150
  """
151
  Separate initialization to allow async
 
169
  # Construct a ChatHub request
170
  self.request.update(prompt=prompt)
171
  # Send request
172
+ self.pause_ping = True
173
  await self.wss.send(append_identifier(self.request.struct))
174
  while True:
175
+ if self.pause:
176
+ await asyncio.sleep(0.2)
177
+ continue
178
  objects = str(await self.wss.recv()).split("")
179
  for obj in objects:
180
  if obj is None or obj == "":
181
  continue
182
  response = json.loads(obj)
183
  if response.get("type") == 2:
184
+ self.pause_ping = False
185
  return response
186
+ self.pause_ping = False
187
 
188
  async def __initial_handshake(self):
189
  await self.wss.send(append_identifier({"protocol": "json", "version": 1}))
 
193
 
194
  async def __ping(self):
195
  while self.loop:
196
+ await asyncio.sleep(5)
197
+ if self.pause_ping:
198
+ continue
199
+ self.pause = True
200
  await self.wss.send(append_identifier({"type": 6}))
201
  await self.wss.recv()
202
+ self.pause = False
203
 
204
  async def close(self):
205
  """