bitunix-automated-crypto-trading 2.6.7__py3-none-any.whl → 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bitunix_automated_crypto_trading/AsyncThreadRunner.py +81 -81
- bitunix_automated_crypto_trading/BitunixApi.py +278 -278
- bitunix_automated_crypto_trading/BitunixSignal.py +1099 -1099
- bitunix_automated_crypto_trading/BitunixWebSocket.py +254 -254
- bitunix_automated_crypto_trading/DataFrameHtmlRenderer.py +74 -74
- bitunix_automated_crypto_trading/NotificationManager.py +23 -23
- bitunix_automated_crypto_trading/ThreadManager.py +68 -68
- bitunix_automated_crypto_trading/TickerManager.py +635 -635
- bitunix_automated_crypto_trading/bitunix.py +597 -594
- bitunix_automated_crypto_trading/config.py +90 -90
- bitunix_automated_crypto_trading/logger.py +84 -84
- {bitunix_automated_crypto_trading-2.6.7.dist-info → bitunix_automated_crypto_trading-3.0.0.dist-info}/METADATA +36 -36
- bitunix_automated_crypto_trading-3.0.0.dist-info/RECORD +17 -0
- bitunix_automated_crypto_trading/config.txt +0 -60
- bitunix_automated_crypto_trading/sampleenv.txt +0 -5
- bitunix_automated_crypto_trading/static/chart.css +0 -28
- bitunix_automated_crypto_trading/static/chart.js +0 -362
- bitunix_automated_crypto_trading/static/modal.css +0 -68
- bitunix_automated_crypto_trading/static/modal.js +0 -147
- bitunix_automated_crypto_trading/static/script.js +0 -166
- bitunix_automated_crypto_trading/static/styles.css +0 -118
- bitunix_automated_crypto_trading/templates/charts.html +0 -98
- bitunix_automated_crypto_trading/templates/login.html +0 -19
- bitunix_automated_crypto_trading/templates/main.html +0 -551
- bitunix_automated_crypto_trading/templates/modal-chart.html +0 -26
- bitunix_automated_crypto_trading/templates/modal-config.html +0 -34
- bitunix_automated_crypto_trading/templates/modal-logs.html +0 -15
- bitunix_automated_crypto_trading-2.6.7.dist-info/RECORD +0 -31
- {bitunix_automated_crypto_trading-2.6.7.dist-info → bitunix_automated_crypto_trading-3.0.0.dist-info}/WHEEL +0 -0
- {bitunix_automated_crypto_trading-2.6.7.dist-info → bitunix_automated_crypto_trading-3.0.0.dist-info}/entry_points.txt +0 -0
- {bitunix_automated_crypto_trading-2.6.7.dist-info → bitunix_automated_crypto_trading-3.0.0.dist-info}/top_level.txt +0 -0
@@ -1,81 +1,81 @@
|
|
1
|
-
import asyncio
|
2
|
-
import threading
|
3
|
-
from
|
4
|
-
import os
|
5
|
-
logger = Logger(__name__).get_logger()
|
6
|
-
|
7
|
-
class AsyncThreadRunner:
|
8
|
-
def __init__(self, async_func, interval, *args, **kwargs):
|
9
|
-
self.async_func = async_func
|
10
|
-
self.interval = interval
|
11
|
-
self.args = args
|
12
|
-
self.kwargs = kwargs
|
13
|
-
self.loop = asyncio.new_event_loop()
|
14
|
-
self._stop_event = threading.Event()
|
15
|
-
self.thread = threading.Thread(target=self.thread_function)
|
16
|
-
self.task = None
|
17
|
-
|
18
|
-
def thread_function(self):
|
19
|
-
asyncio.set_event_loop(self.loop)
|
20
|
-
try:
|
21
|
-
if self.interval == 0:
|
22
|
-
self.task = asyncio.run_coroutine_threadsafe(
|
23
|
-
self.async_func(*self.args, **self.kwargs), self.loop
|
24
|
-
)
|
25
|
-
else:
|
26
|
-
self.task = asyncio.run_coroutine_threadsafe(
|
27
|
-
self.periodic_run(), self.loop
|
28
|
-
)
|
29
|
-
self.loop.run_forever()
|
30
|
-
except Exception as e:
|
31
|
-
logger.info(f"Async Thread function error: {e}")
|
32
|
-
finally:
|
33
|
-
pending = asyncio.all_tasks(self.loop)
|
34
|
-
for task in pending:
|
35
|
-
task.cancel()
|
36
|
-
try:
|
37
|
-
self.loop.run_until_complete(task)
|
38
|
-
except asyncio.CancelledError:
|
39
|
-
pass
|
40
|
-
self.loop.run_until_complete(self.loop.shutdown_asyncgens())
|
41
|
-
self.loop.close()
|
42
|
-
|
43
|
-
async def periodic_run(self):
|
44
|
-
try:
|
45
|
-
while not self._stop_event.is_set():
|
46
|
-
try:
|
47
|
-
await self.async_func(*self.args, **self.kwargs)
|
48
|
-
except Exception as e:
|
49
|
-
logger.info(f"error in periodic_run async thread {self.thread.name} {e}")
|
50
|
-
os._exit(1) # Exit the program if the thread is stopped
|
51
|
-
await asyncio.sleep(self.interval)
|
52
|
-
logger.info(f"periodic {self.thread.name} Thread stopped, exiting app.")
|
53
|
-
os._exit(1) # Exit the program if the thread is stopped
|
54
|
-
except asyncio.CancelledError:
|
55
|
-
pass
|
56
|
-
|
57
|
-
def start_thread(self, thread_name=None):
|
58
|
-
self.thread.name = thread_name
|
59
|
-
self.thread.start()
|
60
|
-
|
61
|
-
async def stop_thread(self):
|
62
|
-
"""Gracefully stop the async thread."""
|
63
|
-
# Signal any periodic task to stop
|
64
|
-
self._stop_event.set()
|
65
|
-
|
66
|
-
# Cancel and await the running task
|
67
|
-
if self.task:
|
68
|
-
self.task.cancel()
|
69
|
-
try:
|
70
|
-
await asyncio.wrap_future(self.task) # Wait for the cancellation to propagate
|
71
|
-
except asyncio.CancelledError:
|
72
|
-
logger.info(f"{self.thread.name} Task cancelled successfully.")
|
73
|
-
except Exception as e:
|
74
|
-
logger.error(f"Unexpected error while cancelling the task {self.thread.name}: {e}")
|
75
|
-
|
76
|
-
# Stop the event loop safely
|
77
|
-
self.loop.call_soon_threadsafe(self.loop.stop)
|
78
|
-
|
79
|
-
# Wait for the thread to join
|
80
|
-
self.thread.join()
|
81
|
-
logger.info(f"{self.thread.name} Thread stopped and event loop cleaned up.")
|
1
|
+
import asyncio
|
2
|
+
import threading
|
3
|
+
from logger import Logger
|
4
|
+
import os
|
5
|
+
logger = Logger(__name__).get_logger()
|
6
|
+
|
7
|
+
class AsyncThreadRunner:
|
8
|
+
def __init__(self, async_func, interval, *args, **kwargs):
|
9
|
+
self.async_func = async_func
|
10
|
+
self.interval = interval
|
11
|
+
self.args = args
|
12
|
+
self.kwargs = kwargs
|
13
|
+
self.loop = asyncio.new_event_loop()
|
14
|
+
self._stop_event = threading.Event()
|
15
|
+
self.thread = threading.Thread(target=self.thread_function)
|
16
|
+
self.task = None
|
17
|
+
|
18
|
+
def thread_function(self):
|
19
|
+
asyncio.set_event_loop(self.loop)
|
20
|
+
try:
|
21
|
+
if self.interval == 0:
|
22
|
+
self.task = asyncio.run_coroutine_threadsafe(
|
23
|
+
self.async_func(*self.args, **self.kwargs), self.loop
|
24
|
+
)
|
25
|
+
else:
|
26
|
+
self.task = asyncio.run_coroutine_threadsafe(
|
27
|
+
self.periodic_run(), self.loop
|
28
|
+
)
|
29
|
+
self.loop.run_forever()
|
30
|
+
except Exception as e:
|
31
|
+
logger.info(f"Async Thread function error: {e}")
|
32
|
+
finally:
|
33
|
+
pending = asyncio.all_tasks(self.loop)
|
34
|
+
for task in pending:
|
35
|
+
task.cancel()
|
36
|
+
try:
|
37
|
+
self.loop.run_until_complete(task)
|
38
|
+
except asyncio.CancelledError:
|
39
|
+
pass
|
40
|
+
self.loop.run_until_complete(self.loop.shutdown_asyncgens())
|
41
|
+
self.loop.close()
|
42
|
+
|
43
|
+
async def periodic_run(self):
|
44
|
+
try:
|
45
|
+
while not self._stop_event.is_set():
|
46
|
+
try:
|
47
|
+
await self.async_func(*self.args, **self.kwargs)
|
48
|
+
except Exception as e:
|
49
|
+
logger.info(f"error in periodic_run async thread {self.thread.name} {e}")
|
50
|
+
os._exit(1) # Exit the program if the thread is stopped
|
51
|
+
await asyncio.sleep(self.interval)
|
52
|
+
logger.info(f"periodic {self.thread.name} Thread stopped, exiting app.")
|
53
|
+
os._exit(1) # Exit the program if the thread is stopped
|
54
|
+
except asyncio.CancelledError:
|
55
|
+
pass
|
56
|
+
|
57
|
+
def start_thread(self, thread_name=None):
|
58
|
+
self.thread.name = thread_name
|
59
|
+
self.thread.start()
|
60
|
+
|
61
|
+
async def stop_thread(self):
|
62
|
+
"""Gracefully stop the async thread."""
|
63
|
+
# Signal any periodic task to stop
|
64
|
+
self._stop_event.set()
|
65
|
+
|
66
|
+
# Cancel and await the running task
|
67
|
+
if self.task:
|
68
|
+
self.task.cancel()
|
69
|
+
try:
|
70
|
+
await asyncio.wrap_future(self.task) # Wait for the cancellation to propagate
|
71
|
+
except asyncio.CancelledError:
|
72
|
+
logger.info(f"{self.thread.name} Task cancelled successfully.")
|
73
|
+
except Exception as e:
|
74
|
+
logger.error(f"Unexpected error while cancelling the task {self.thread.name}: {e}")
|
75
|
+
|
76
|
+
# Stop the event loop safely
|
77
|
+
self.loop.call_soon_threadsafe(self.loop.stop)
|
78
|
+
|
79
|
+
# Wait for the thread to join
|
80
|
+
self.thread.join()
|
81
|
+
logger.info(f"{self.thread.name} Thread stopped and event loop cleaned up.")
|