thread_test_for_sanjiang.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import os
  4. import sys
  5. import threading
  6. import time
  7. PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
  8. sys.path.insert(0, PROJECT_ROOT)
  9. os.environ.update({"DJANGO_SETTINGS_MODULE": "configs.testing"})
  10. import django
  11. django.setup()
  12. from apps.web.core.helpers import ActionDeviceBuilder
  13. from apps.web.device.models import Device
  14. class Tester(threading.Thread):
  15. def __init__(self,sleepTime, runTimes,logicalCode,func,**args):
  16. super(Tester, self).__init__()
  17. dev = Device.get_dev_by_l(logicalCode)
  18. self._smartBox = ActionDeviceBuilder.create_action_device(dev)
  19. self._func = func
  20. self._args = args
  21. self._runTimes = runTimes
  22. self._sleepTime = sleepTime
  23. def run(self):
  24. count = 0
  25. while count < self._runTimes:
  26. count += 1
  27. try:
  28. result = eval('self._smartBox.%s(**self._args)' % self._func)
  29. # result = self._smartBox.get_port_status_from_dev()
  30. print result
  31. if self._sleepTime:
  32. time.sleep(self._sleepTime)
  33. except Exception,e:
  34. if u'充电桩正在忙' in e.result.get('description'):
  35. print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!error = %s,func=%s' % (e,self._func))
  36. logicalCode = '865650042312401'
  37. ii = 0
  38. # for ii in range(10):
  39. # Tester(0,100000,logicalCode,'get_port_status_from_dev').start()
  40. # #
  41. # ii = 0
  42. # for ii in range(10):
  43. # Tester(0,100000,logicalCode,'get_port_info',line=ii%10).start()
  44. # #
  45. # #
  46. # #
  47. # ii = 0
  48. # for ii in range(2):
  49. # Tester(0,100000,logicalCode,'get_dev_consume_count').start()
  50. # #
  51. # ii = 0
  52. # for ii in range(2):
  53. # Tester(0,100000,logicalCode,'get_dev_setting').start()
  54. ii = 0
  55. for ii in range(9):
  56. Tester(1,1000,logicalCode,'start_device',package={'time':3,'unit':u'分钟','coins':1,'price':1},openId='aaaaaaaaaaaaaa',attachParas = {'chargeIndex':ii}).start()
  57. # ii = 0
  58. # for ii in range(10):
  59. # Tester(200,100000,logicalCode,'stop_charging_port',port = ii).start()
  60. print 'finished'