-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground_example.py
More file actions
66 lines (47 loc) · 1.56 KB
/
background_example.py
File metadata and controls
66 lines (47 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
FCS Background Thread Example
Shows how to run WebSocket in background thread (non-blocking).
This allows your main code to continue running while receiving price updates.
Run: python background_example.py
"""
import sys
import os
import time
# Add parent directory to path for import
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from fcs_client_lib import FCSClient
# Create client with demo API key
client = FCSClient('fcs_socket_demo')
# Store latest prices
latest_prices = {}
@client.on_connected
def on_connected():
print('[WebSocket] Connected!')
client.join('BINANCE:BTCUSDT', '1D')
client.join('BINANCE:ETHUSDT', '1D')
@client.on_message
def on_message(data):
if data.get('type') == 'price':
symbol = data.get('symbol')
price = data['prices'].get('c')
latest_prices[symbol] = price
if __name__ == '__main__':
print('FCS WebSocket - Background Thread Example')
print('=' * 50)
# Connect and run in background (non-blocking)
client.connect()
client.run_forever(blocking=False)
print('[Main] WebSocket running in background thread')
print('[Main] Main thread continues to run...\n')
# Wait for connection
time.sleep(3)
# Main loop - do your work here while prices update in background
try:
for i in range(20):
print(f'[Main] Tick {i + 1}/20 - Latest prices: {latest_prices}')
time.sleep(3)
except KeyboardInterrupt:
pass
print('\n[Main] Disconnecting...')
client.disconnect()
print('[Main] Done!')