Advertisement
retesere20

tmp-debug-py

Oct 8th, 2024 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. import os, asyncio, json, sys, psutil, jsonpickle
  2. targetDir1 = os.path.dirname('./c/python/')
  3. sys.path.append(targetDir1)
  4. import ccxt
  5. import ccxt.async_support as ccxta
  6.  
  7. # asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
  8.  
  9. current_file = (os.path.basename(__file__))
  10.  
  11. def write_content (filename, content, mode='w', encoding='utf-8'):
  12. try:
  13. with open(filename, mode, encoding=encoding) as file:
  14. if isinstance(content, list):
  15. # If content is a list, write each item as a line
  16. file.writelines(content)
  17. else:
  18. # If content is a string, write it directly
  19. file.write(content)
  20. print(f"Successfully wrote to {filename}")
  21. return True
  22. except IOError as e:
  23. # Handle potential IO errors like permission issues or disk full
  24. print(f"An error occurred while writing to the file: {e}")
  25. return False
  26. except Exception as e:
  27. # Catch any other unexpected errors
  28. print(f"Unexpected error: {e}")
  29. return False
  30.  
  31. def write_obj(filename, obj, mode='w', encoding='utf-8'):
  32. newobj = vars(obj)
  33. content = jsonpickle.encode(newobj, max_depth=10)
  34. return write_content(filename, content, mode, encoding)
  35.  
  36. def write_json(filename, obj, mode='w', encoding='utf-8'):
  37. content = json.dumps(obj)
  38. return write_content(filename, content, mode, encoding)
  39.  
  40.  
  41. def memory_usage_psutil():
  42. # Get the current process
  43. process = psutil.Process(os.getpid())
  44. # Return memory usage in MB
  45. memory_usage = process.memory_info().rss / (1024 * 1024)
  46. return memory_usage
  47.  
  48. initial_memory = memory_usage_psutil()
  49.  
  50. def print_mem(stage):
  51. current = memory_usage_psutil() - initial_memory
  52. print(f"Memory #{stage}: {current:.2f} MB")
  53.  
  54. #################################################
  55.  
  56.  
  57. dict_keys = ['markets', 'markets_by_id', 'symbols', 'currencies', 'currencies_by_id', 'baseCurrencies', 'quoteCurrencies', 'codes', 'ids', 'last_http_response', 'last_json_response', 'last_response_headers', 'last_request_headers']
  58.  
  59. def get_dicts(ex):
  60. final = {}
  61. for key in dict_keys:
  62. final[key] = getattr(ex, key)
  63. return final.copy()
  64.  
  65. def clear_dicts(e):
  66. for key in dict_keys:
  67. setattr(e, key, None)
  68.  
  69. def clear_session(e):
  70. e.asyncio_loop = None
  71. e.throttle.loop = None
  72. e.session = None
  73. e.tcp_connector = None
  74.  
  75.  
  76. async def reload_ex(e, stage, reset = False, close = False, mkt = True):
  77. stage = str(stage)
  78. if (mkt):
  79. await e.load_markets(reload=True)
  80. print_mem("RELOADED " + stage)
  81. cur = get_dicts(e)
  82. write_json(current_file + '-' + stage + '-dicts.txt', cur)
  83. clear_dicts(e)
  84. if (reset):
  85. clear_session(e)
  86. await asyncio.sleep(5) # wait for potential GC
  87. print_mem ("RESET " + stage)
  88. write_obj(current_file + '-' + stage + '-class.txt', e)
  89. if (close):
  90. await e.close()
  91. await asyncio.sleep(5) # wait for potential GC
  92. print_mem("CLOSED " + stage)
  93.  
  94.  
  95.  
  96. #################################################
  97.  
  98. async def main():
  99. print_mem("before CONSTRUCT")
  100. e = ccxta.binance()
  101. print_mem("after CONSTRUCT")
  102. await reload_ex(e, 0, False, False, False)
  103. await reload_ex(e, 1)
  104. await reload_ex(e, 2)
  105. await reload_ex(e, 3)
  106. await reload_ex(e, 4, False, True)
  107. await reload_ex(e, 5, False, True)
  108. await reload_ex(e, 6, True)
  109. await reload_ex(e, 7, True)
  110. await reload_ex(e, 8, True)
  111. # last one to clear all things manually
  112. clear_dicts(e)
  113. clear_session(e)
  114. e = None
  115. del e
  116. await asyncio.sleep(5) # wait for potential GC
  117. print_mem("AFTER NULL")
  118.  
  119. asyncio.run(main())
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement