MCPcopy Create free account
hub / github.com/miguelgrinberg/python-socketio / call

Method call

src/socketio/server.py:213–270  ·  view source on GitHub ↗

Emit a custom event to a client and wait for the response. This method issues an emit with a callback and waits for the callback to be invoked before returning. If the callback isn't invoked before the timeout, then a ``TimeoutError`` exception is raised. If the Sock

(self, event, data=None, to=None, sid=None, namespace=None,
             timeout=60, ignore_queue=False)

Source from the content-addressed store, hash-verified

211 ignore_queue=ignore_queue)
212
213 def call(self, event, data=None, to=None, sid=None, namespace=None,
214 timeout=60, ignore_queue=False):
215 """Emit a custom event to a client and wait for the response.
216
217 This method issues an emit with a callback and waits for the callback
218 to be invoked before returning. If the callback isn't invoked before
219 the timeout, then a ``TimeoutError`` exception is raised. If the
220 Socket.IO connection drops during the wait, this method still waits
221 until the specified timeout.
222
223 :param event: The event name. It can be any string. The event names
224 ``'connect'``, ``'message'`` and ``'disconnect'`` are
225 reserved and should not be used.
226 :param data: The data to send to the client or clients. Data can be of
227 type ``str``, ``bytes``, ``list`` or ``dict``. To send
228 multiple arguments, use a tuple where each element is of
229 one of the types indicated above.
230 :param to: The session ID of the recipient client.
231 :param sid: Alias for the ``to`` parameter.
232 :param namespace: The Socket.IO namespace for the event. If this
233 argument is omitted the event is emitted to the
234 default namespace.
235 :param timeout: The waiting timeout. If the timeout is reached before
236 the client acknowledges the event, then a
237 ``TimeoutError`` exception is raised.
238 :param ignore_queue: Only used when a message queue is configured. If
239 set to ``True``, the event is emitted to the
240 client directly, without going through the queue.
241 This is more efficient, but only works when a
242 single server process is used. It is recommended
243 to always leave this parameter with its default
244 value of ``False``.
245
246 Note: this method is not thread safe. If multiple threads are emitting
247 at the same time to the same client, then messages composed of
248 multiple packets may end up being sent in an incorrect sequence. Use
249 standard concurrency solutions (such as a Lock object) to prevent this
250 situation.
251 """
252 if to is None and sid is None:
253 raise ValueError('Cannot use call() to broadcast.')
254 if not self.async_handlers:
255 raise RuntimeError(
256 'Cannot use call() when async_handlers is False.')
257 callback_event = self.eio.create_event()
258 callback_args = []
259
260 def event_callback(*args):
261 callback_args.append(args)
262 callback_event.set()
263
264 self.emit(event, data=data, room=to or sid, namespace=namespace,
265 callback=event_callback, ignore_queue=ignore_queue)
266 if not callback_event.wait(timeout=timeout):
267 raise exceptions.TimeoutError()
268 return callback_args[0] if len(callback_args[0]) > 1 \
269 else callback_args[0][0] if len(callback_args[0]) == 1 \
270 else None

Callers 4

test_callMethod · 0.95

Calls 2

emitMethod · 0.95
waitMethod · 0.45

Tested by 4

test_callMethod · 0.76