add.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-
  3. import sys
  4. sys.path.append(r"D:\ProgramData\Anaconda2\Lib")
  5. sys.path.append(r"D:\vi\vi_server")
  6. #import ibm_ai
  7. from multiprocessing import Process,Lock
  8. import time
  9. from multiprocessing import Pool,Manager
  10. import json
  11. import os
  12. class Add:
  13. _total=0
  14. def __init__(self,a,b):
  15. self._total=a+b
  16. print("total:",self._total)
  17. def add(self,c):
  18. self._total+=c
  19. print("total:",self._total)
  20. def sub(self,c):
  21. self._total-=c
  22. print("total:",self._total)
  23. def get_total(self):
  24. return self._total
  25. # add=Add(1,2)
  26. # def add_c(c):
  27. # global add
  28. # add.add(c)
  29. # return add.get_total()
  30. # def sub_c(c):
  31. # global add
  32. # add.sub(c)
  33. # return add.get_total()
  34. # def get_cwd():
  35. # # json1={'a':'1'}
  36. # # json2={'b':'2'}
  37. # # json=ibm_ai.merge_json(json1,json2)
  38. # return os.getcwd()
  39. #print("hello world total:",add_c(1))
  40. def foo(n,queue):
  41. # print("ok")
  42. # if lock is None:
  43. # print("lock is None")
  44. time.sleep(6-n)
  45. #lock.acquire()
  46. #print('In process:', n, os.getpid())
  47. #lock.release()
  48. queue.put("AI_result:"+str(n))
  49. return n
  50. def bar(*args):
  51. results.append(args[0])
  52. print('>>done: ', args, os.getpid())
  53. def l(num,lock,p):
  54. lock.acquire()
  55. #stime.sleep(2)
  56. print ("Hello num:%s" ,num)
  57. lock.release()
  58. results=[]
  59. if __name__=='__main__':
  60. json_str="{\"shape_type\":\"line\",\"shape_data\":[[12,1],[12,2]],\"value\":\"\",\"action\":\"add\"}"
  61. # j={"shape_data":[(1,2),(3,4)]}
  62. # str_j=json.dumps(j)
  63. #print("str_j:",str_j)
  64. # json_obj=json.loads(json_str)
  65. # print(json_obj["shape_data"][0][0])
  66. # lock=Lock()
  67. # pool = Pool(processes=3)
  68. # json_obj={"j":lock}
  69. # queue = Manager().Queue()
  70. # concurrency=6
  71. # for num in range(concurrency):
  72. # #Process(target=foo,args=({"i":num}, )).start()
  73. # pool.apply_async(func=foo, args=(num,queue))
  74. # print("async:",num)
  75. # num=0
  76. # while num<concurrency:
  77. # res= queue.get()
  78. # print("res:",res)
  79. # num+=1
  80. # time.sleep(0.01)
  81. # pool.close()
  82. # pool.join()
  83. # if __name__ == "__main__":
  84. # pool = Pool(processes=3)
  85. # lock=Lock()
  86. # print('main process:', os.getpid())
  87. # for i in range(6):
  88. # # pool.apply(func=foo, args=(i, ))
  89. # pool.apply_async(func=foo, args=({"i":i},), callback=bar)
  90. # print('end')
  91. # pool.close()
  92. # pool.join()
  93. # p = multiprocessing.Process(target=process_run, args=(i,))
  94. # p.start()