You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tasks_loop.py 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import asyncio
  2. import time
  3. import redis
  4. import os
  5. import copy
  6. import json
  7. import traceback
  8. import inspect
  9. '''
  10. umod
  11. # '''
  12. p=print
  13. async def anop(*a,**kw):pass
  14. def nop(*a,**kw):pass
  15. def connect_redis():
  16. return redis.Redis(host='localhost', port=6379, db=0,charset="utf-8", decode_responses=True, encoding_errors="backslashreplace")
  17. '''
  18. # '''
  19. def callable_helper(fn):
  20. ret = {
  21. "is_callable":0,
  22. "is_coroutine":0,
  23. }
  24. ret["is_callable"]= callable(fn)
  25. ret["is_coroutine"]= inspect.iscoroutinefunction(fn)
  26. # if (ca)
  27. return ret
  28. verbose = print
  29. class DbgMixin:
  30. async def ws_rec(self,text_data,*a,**kw):
  31. # if "dbg" in
  32. text_data_json = json.loads(text_data)
  33. if not 'dbg' in text_data_json:
  34. return
  35. p("DBG!")
  36. exec(text_data_json["dbg"])
  37. class AconMixin(DbgMixin):
  38. # class AconMixin:
  39. async def connect(self):
  40. # print("-----------CONNECTING async def connect")
  41. p("ACON!!!!!!!!!!!!!!!!!!!!!!!!!!!")
  42. p(flush=True)
  43. await self.accept()
  44. verbose("<MRO")
  45. verbose(type(self))
  46. await self.call_all_mro("ws_conn0")
  47. await self.call_all_mro("ws_conn")
  48. await self.call_all_mro("ws_conn2")
  49. verbose("/MRO>")
  50. # await self.ws_conn_once(event)
  51. await self.ws_conn_once()
  52. async def ws_conn_once(self, *a,**kw):pass
  53. async def ws_disconn_once(self, *a,**kw):pass
  54. async def ws_rec_once(self, *a,**kw):pass
  55. async def receive(self, *a,**kw):
  56. # print("REC")
  57. # print("-----------REC")
  58. await self.call_all_mro("ws_rec",*a,**kw)
  59. await self.ws_rec_once(*a,**kw)
  60. async def call_all_mro(self,mthd_name,*args,**kwargs):
  61. called = set([None])
  62. for cls_obj in self.__class__.__mro__:
  63. mthd = getattr(cls_obj,mthd_name,None)
  64. # print("~",cls_obj,mthd)
  65. if not mthd in called:
  66. # print(cls_obj,mthd)
  67. called.add(mthd)
  68. await mthd(self,*args,**kwargs)
  69. async def disconnect(self, close_code):
  70. await self.call_all_mro("ws_disconn",close_code)
  71. await self.ws_disconn_once(close_code)
  72. async def websocket_connect_x(self, event):
  73. await super().websocket_connect(event)
  74. print("<MRO")
  75. print(type(self))
  76. await self.call_all_mro("ws_conn",event)
  77. await self.call_all_mro("ws_conn2",event)
  78. print("/MRO>")
  79. await self.ws_conn_once(event)
  80. class AclTaskMixin:
  81. async def ws_conn0(self):
  82. glob_cl.run_persistent_loop(self)
  83. print("++++++++++++++++++++++++++")
  84. # oinfo(glob_cl)
  85. async def ws_conn_once(self,*a,**kw):
  86. print("\t\tws_conn_once:","AclTaskMixin")
  87. # GCLMixin
  88. class globCL():
  89. def __init__(self,*a,**kw):
  90. # for now we are not going to do any weird class stuff
  91. self.rconn = connect_redis()
  92. self.rman = RldMan()
  93. self.cbs_once = []
  94. self.cbs_once_set = set()
  95. self.cbs = {"fn1":fn1,"fn2x":nop}
  96. self.timeout = 1
  97. pass
  98. async def persistent_loop(self,*a,**kw):
  99. while 1:
  100. # p("PL:",time.time(),flush=1)
  101. # await asyncio.sleep(5)
  102. # for k,v in cls.cbs.items():
  103. for k,v in [*self.cbs.items()]:
  104. try:
  105. call_info = callable_helper(v)
  106. if (call_info["is_callable"]):
  107. if call_info["is_coroutine"]:
  108. pass
  109. await v({k,self})
  110. else:
  111. v({k,self})
  112. else:
  113. pass
  114. # p(k,v)
  115. pass
  116. except Exception as e:
  117. p("persistent_loop Exception",e)
  118. p(flush=True)
  119. # raise
  120. else:
  121. pass
  122. finally:
  123. pass
  124. # cbs_once
  125. cbs_once = [*self.cbs_once,*self.cbs_once_set]
  126. self.cbs_once = []
  127. self.cbs_once_set = set()
  128. k=0
  129. for v in cbs_once:
  130. try:
  131. call_info = callable_helper(v)
  132. if (call_info["is_callable"]):
  133. if call_info["is_coroutine"]:
  134. pass
  135. await v({k,self})
  136. else:
  137. v({k,self})
  138. else:
  139. pass
  140. # p(k,v)
  141. pass
  142. except Exception as e:
  143. p("persistent_loop Exception cbs_once:",e)
  144. p(flush=True)
  145. # raise
  146. else:
  147. pass
  148. finally:
  149. pass
  150. k += 1
  151. await asyncio.sleep(self.timeout)
  152. def _run_persistent_loop(self,*a,**kw):
  153. p("_run_persistent_loop:")
  154. loop = asyncio.get_event_loop()
  155. self.run_persistent_loop = nop
  156. loop.create_task(self.persistent_loop())
  157. self.loop = loop
  158. run_persistent_loop = _run_persistent_loop
  159. # RldManMixin
  160. class RldMan():
  161. def __init__(self,*a,**kw):
  162. self.files = {}
  163. self.print_tb = 0
  164. self.dflt_scope = {"globals":globals(),"locals":locals()}
  165. self.dflt_scope2 = {"globals":copy.copy(globals()),"locals":copy.copy(locals())}
  166. base_path = "/usr/games/repos/ign4/py_rld/djc/rdir1/"
  167. file_list = [
  168. base_path+"r0.py",
  169. base_path+"r1.py",
  170. base_path+"r2.py",
  171. base_path+"r3.py",
  172. base_path+"r4.py",
  173. base_path+"r5.py",
  174. ]
  175. self.add_files(file_list)
  176. def add_files(self,files):
  177. for file in files:
  178. if type(file)==str:
  179. self.add_file(file)
  180. elif type(file)==list:
  181. self.add_file(*file)
  182. else:
  183. p("add files???",file)
  184. def add_file(self,file_name,fnx={}):
  185. self.files[file_name] = {"ftxt":"",**fnx}
  186. def rld_file(self,file_name,ret= {"errs":{},"all":{},"alle":{},},**kw):
  187. if file_name in self.files:
  188. rfile_obj = self.files[file_name]
  189. st = os.stat(file_name)
  190. st_tuple = (st.st_mtime,st.st_size)
  191. if rfile_obj["ftxt"] != st_tuple:
  192. rfile_obj["ftxt"] = st_tuple
  193. try:
  194. f = open(file_name,"r")
  195. ftxt = f.read()
  196. f.close()
  197. exec(ftxt)
  198. except Exception as e:
  199. print("EXCEPT",eflag,e)
  200. if self.print_tb:
  201. traceback.print_tb(e.__traceback__,file=sys.stdout)
  202. else:
  203. pass
  204. # print("ELSE")
  205. finally:
  206. # print("FINALLY")
  207. pass
  208. print(end="",flush=True)
  209. return ret
  210. def rld_files(self):
  211. # p()
  212. ret = {
  213. "errs":{},
  214. "all":{},
  215. "alle":{},
  216. }
  217. # for k in self.files:
  218. for k in [*self.files]:
  219. self.rld_file(k,ret)
  220. # p("rld_files!")
  221. p("",end="",flush=True)
  222. return ret
  223. def fn1(*a,**kw):
  224. # rld_files
  225. glob_cl.rman.rld_files()
  226. glob_cl = globCL()
  227. # print("..",end="\n",flush=True)
  228. def redis_info():
  229. rconn = glob_cl.rconn
  230. keys = rconn.keys()
  231. for k in keys:
  232. p(":",k)
  233. p("len(keys)",len(keys))
  234. print("",end="",flush=True)
  235. # rconn.flushall()
  236. redis_info()
  237. r"""
  238. def atexit_fn():
  239. pass
  240. # rconn.flushall()
  241. print("ATEXIT FN")
  242. rinfo_len()
  243. keys = rconn.keys()
  244. print(keys)
  245. print(len(keys))
  246. print("t 000")
  247. # time.sleep(10)
  248. print("t 010")
  249. # time.sleep(20)
  250. print("t 020")
  251. print("ATEXIT FN...")
  252. rconn.flushall()
  253. atexit.register(atexit_fn)
  254. # """