ok
CHOOSE YOUR LANGUAGE

Synchronous and Asynchronous

PyZenbo SDK provide synchronous and asynchronous parameter for most of the functions. The default is synchronous.

  • Synchronous: Wait function finished and return value
  • Asynchronous: don’t wait and process next function. Result is returned by callback.

How to use asynchronous callback?

Same as ZenboSDK in Java, there are two callback functions.

  • on_state_change: get function returning, include status and error code
  • on_result: get value for function result

Sample Code: move_body.py

Open PyZenbo/tests/move_body.py

import pyzenbo
from pyzenbo.modules.error_code import code_to_description

host = '192.168.0.186'
sdk = pyzenbo.connect(host)

def on_state_change(*args):
    serial, cmd, error, state = args
    msg = 'on_state_change serial:{}, cmd:{}, error:{}, state:{}'
    print(msg.format(serial, cmd, error, state))
    print('on_state_change error:', code_to_description(error))

def on_result(*args):
    print('on_result', args)

sdk.on_state_change_callback = on_state_change
sdk.on_result_callback = on_result

result = sdk.motion.move_body(10, 0, 0, sync=False, timeout=None)
print(result)
result = sdk.motion.move_body(10, 0, 0, sync=True, timeout=100)
print(result)

  1. The first move_body is the asynchronous function. You can get the result from on_state_chang callback. It processes the second move_body (serial:2) and return PENDING immediately.
  2. The second move_body is the synchronous function. You can get returning from result.
    • Result type is tuple. (2, {'state': 4, 'error': 33554548})
    • The first element is serial number
    • The second element is dict. This dict include “status” and “error”
    • STATE = {0: 'INITIAL', 1: 'PENDING', 2: 'REJECTED', 3: 'ACTIVE', 4: 'FAILED', 5: 'SUCCEED', 6: 'PREEMPTED', }
    • You can get error description by py_zenbo_sdk.modules.error_code.

On the other hand, you can set timeout parameter if you expect the ending time of the function. It will cancel the action when timeout.

 

Go To Top