Python程序同时运行功能,而不是顺序运行
使用以下程序,我正在尝试
- 使用binance-connector lib创建订单,然后
- 使用用户数据套接字流(Unicorn-binance-websocket-api)检查创建订单的状态,
- 一旦我从套接字流中获得更新,则订单的填充,我想关闭流并继续以创建下一个订单。
我面临的问题是,当Websocket流正在运行时,该程序正在继续创建第二订单,而无需等待WebSocket连接停止。它基本上是同时运行流()和main_algo()。鉴于,我希望stream()在使用main_algo()下的下一行之前完成。我要去哪里?
from binance.futures import Futures
from unicorn_binance_websocket_api.manager import BinanceWebSocketApiManager
import threading
binance_websocket_api_manager = BinanceWebSocketApiManager(exchange="binance.com-futures-testnet", throw_exception_if_unrepairable=True)
stream_id = binance_websocket_api_manager.create_stream('arr', '!userData', api_key=api_key, api_secret=secret_key)
client = Futures(api_key, secret_key, base_url="https://testnet.binancefuture.com")
def stream(binance_websocket_api_manager):
global order_id
while True:
if binance_websocket_api_manager.is_manager_stopping():
exit(0)
oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
if oldest_stream_data_from_stream_buffer is False:
time.sleep(0.01)
else:
print(oldest_stream_data_from_stream_buffer)
# if status of created order is filled:
binance_websocket_api_manager.stop_manager_with_all_streams() # stop connection
def order_status():
worker_thread = threading.Thread(target=stream, args=(binance_websocket_api_manager,))
worker_thread.start()
def main_algo():
client.new_order(symbol, price, quantity, 'BUY') #(1st Order)
order_status() #start websocket to check for order status
print('first order filled')
#create next order
print('creating second order')
client.new_order(symbol, price, quantity, 'BUY') #(2nd Order)
main_algo()
With the below program I am trying to
- Create an order using binance-connector lib then
- Check status of the created order using user data socket streaming(unicorn-binance-websocket-api),
- Once I get an update from the socket stream that the status of the order is filled, I want to close the stream and move on to create the next order.
The issue I am facing is that, while the websocket stream is running, the program is moving on to create the 2nd order without waiting for the websocket connection to stop. It is basically running stream() and main_algo() simultaneously. Whereas, I want the stream() to finish before proceeding with next line under main_algo(). Where am I going wrong?
from binance.futures import Futures
from unicorn_binance_websocket_api.manager import BinanceWebSocketApiManager
import threading
binance_websocket_api_manager = BinanceWebSocketApiManager(exchange="binance.com-futures-testnet", throw_exception_if_unrepairable=True)
stream_id = binance_websocket_api_manager.create_stream('arr', '!userData', api_key=api_key, api_secret=secret_key)
client = Futures(api_key, secret_key, base_url="https://testnet.binancefuture.com")
def stream(binance_websocket_api_manager):
global order_id
while True:
if binance_websocket_api_manager.is_manager_stopping():
exit(0)
oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
if oldest_stream_data_from_stream_buffer is False:
time.sleep(0.01)
else:
print(oldest_stream_data_from_stream_buffer)
# if status of created order is filled:
binance_websocket_api_manager.stop_manager_with_all_streams() # stop connection
def order_status():
worker_thread = threading.Thread(target=stream, args=(binance_websocket_api_manager,))
worker_thread.start()
def main_algo():
client.new_order(symbol, price, quantity, 'BUY') #(1st Order)
order_status() #start websocket to check for order status
print('first order filled')
#create next order
print('creating second order')
client.new_order(symbol, price, quantity, 'BUY') #(2nd Order)
main_algo()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论