示例

完整的示例请参考sample。以下是简单的示例:

  1. #!/usr/bin/env python
  2. # coding:utf-8
  3. from pypegasus.pgclient import Pegasus
  4. from twisted.internet import reactor
  5. from twisted.internet.defer import inlineCallbacks
  6. @inlineCallbacks
  7. def basic_test():
  8. # init
  9. c = Pegasus(['127.0.0.1:34601', '127.0.0.1:34602'], 'temp')
  10. suc = yield c.init()
  11. if not suc:
  12. reactor.stop()
  13. print('ERROR: connect pegasus server failed')
  14. return
  15. # set
  16. try:
  17. ret = yield c.set('hkey1', 'skey1', 'value', 0, 500)
  18. print('set ret: ', ret)
  19. except Exception as e:
  20. print(e)
  21. # get
  22. ret = yield c.get('hkey1', 'skey1')
  23. print('get ret: ', ret)
  24. reactor.stop()
  25. if __name__ == "__main__":
  26. reactor.callWhenRunning(basic_test)
  27. reactor.run()