7.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import allspark
  2. class MyProcessor(allspark.BaseProcessor):
  3. """ MyProcessor is a example
  4. you can send mesage like this to predict
  5. curl -v http://127.0.0.1:8080/api/predict/service_name -d '2 105'
  6. """
  7. def initialize(self):
  8. """ load module, executed once at the start of the service
  9. do service intialization and load models in this function.
  10. """
  11. self.module = {'w0': 100, 'w1': 2}
  12. def pre_proccess(self, data):
  13. """ data format pre process
  14. """
  15. x, y = data.split(b' ')
  16. return int(x), int(y)
  17. def post_process(self, data):
  18. """ proccess after process
  19. """
  20. return bytes(data, encoding='utf8')
  21. def process(self, data):
  22. """ process the request data
  23. """
  24. x, y = self.pre_proccess(data)
  25. w0 = self.module['w0']
  26. w1 = self.module['w1']
  27. y1 = w1 * x + w0
  28. if y1 >= y:
  29. return self.post_process("True"), 200
  30. else:
  31. return self.post_process("False"), 400
  32. if __name__ == '__main__':
  33. # paramter worker_threads indicates concurrency of processing
  34. runner = MyProcessor(worker_threads=10)
  35. runner.run()