Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, asyncio, json, sys, psutil, jsonpickle
- targetDir1 = os.path.dirname('./c/python/')
- sys.path.append(targetDir1)
- import ccxt
- import ccxt.async_support as ccxta
- # asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
- current_file = (os.path.basename(__file__))
- def write_content (filename, content, mode='w', encoding='utf-8'):
- try:
- with open(filename, mode, encoding=encoding) as file:
- if isinstance(content, list):
- # If content is a list, write each item as a line
- file.writelines(content)
- else:
- # If content is a string, write it directly
- file.write(content)
- print(f"Successfully wrote to {filename}")
- return True
- except IOError as e:
- # Handle potential IO errors like permission issues or disk full
- print(f"An error occurred while writing to the file: {e}")
- return False
- except Exception as e:
- # Catch any other unexpected errors
- print(f"Unexpected error: {e}")
- return False
- def write_obj(filename, obj, mode='w', encoding='utf-8'):
- newobj = vars(obj)
- content = jsonpickle.encode(newobj, max_depth=10)
- return write_content(filename, content, mode, encoding)
- def write_json(filename, obj, mode='w', encoding='utf-8'):
- content = json.dumps(obj)
- return write_content(filename, content, mode, encoding)
- def memory_usage_psutil():
- # Get the current process
- process = psutil.Process(os.getpid())
- # Return memory usage in MB
- memory_usage = process.memory_info().rss / (1024 * 1024)
- return memory_usage
- initial_memory = memory_usage_psutil()
- def print_mem(stage):
- current = memory_usage_psutil() - initial_memory
- print(f"Memory #{stage}: {current:.2f} MB")
- #################################################
- 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']
- def get_dicts(ex):
- final = {}
- for key in dict_keys:
- final[key] = getattr(ex, key)
- return final.copy()
- def clear_dicts(e):
- for key in dict_keys:
- setattr(e, key, None)
- def clear_session(e):
- e.asyncio_loop = None
- e.throttle.loop = None
- e.session = None
- e.tcp_connector = None
- async def reload_ex(e, stage, reset = False, close = False, mkt = True):
- stage = str(stage)
- if (mkt):
- await e.load_markets(reload=True)
- print_mem("RELOADED " + stage)
- cur = get_dicts(e)
- write_json(current_file + '-' + stage + '-dicts.txt', cur)
- clear_dicts(e)
- if (reset):
- clear_session(e)
- await asyncio.sleep(5) # wait for potential GC
- print_mem ("RESET " + stage)
- write_obj(current_file + '-' + stage + '-class.txt', e)
- if (close):
- await e.close()
- await asyncio.sleep(5) # wait for potential GC
- print_mem("CLOSED " + stage)
- #################################################
- async def main():
- print_mem("before CONSTRUCT")
- e = ccxta.binance()
- print_mem("after CONSTRUCT")
- await reload_ex(e, 0, False, False, False)
- await reload_ex(e, 1)
- await reload_ex(e, 2)
- await reload_ex(e, 3)
- await reload_ex(e, 4, False, True)
- await reload_ex(e, 5, False, True)
- await reload_ex(e, 6, True)
- await reload_ex(e, 7, True)
- await reload_ex(e, 8, True)
- # last one to clear all things manually
- clear_dicts(e)
- clear_session(e)
- e = None
- del e
- await asyncio.sleep(5) # wait for potential GC
- print_mem("AFTER NULL")
- asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement