英文:
Python Code gets stuck, not even the CTRL+C helps to exit
问题
I have written code to receive Live market data using Websockets, I analyze the data from within the main Loop (with a sleep time of 0.1 seconds between iterations) and take trading decisions based on the current market levels, from within the main loop I also print the stats every 10 seconds or so. Entire code gets executed within 0.001 seconds (except in some rare cases), so each loop takes about 0.101 seconds (thought these numbers might be helpful). The code runs for 375 minutes a day, I mostly use Visual Code Studio to Write/ Edit and Run code, on Windows 10 64 bit.
The problem I'm facing is that the execution freezes sometimes, The code doesn't print updates, It doesn't execute anything at all, goes comatose. I can't get out of it even by pressing CTRL+C. If the problem was because of the command getting lost in some corner of the code in an eternal loop, we should have been able to exit it by pressing CTRL+C right? At first I thought this is Visual Studio Code problem, the same happens when I try to Run the code through IDLE
Seems like some basic Python issue, which the knowledgeable coders might be able to pinpoint, Please help me find the bug. Thanks in advance
I tried steps to speed up Visual studio Code, uninstalled extensions that I don't need. But it doesn't look like VSC problem. I'm out of ideas
EDIT:
I have come to the understanding that the problem is most likely happening because of an http request to the broker that doesn't return an acknowledgement (The broker confirmed that the order would be in eternal wait without either a rejection or completion)
So here is what the sample code looks like:
import time
import api
is_in_position=0
def isExit():
# If exit is necessary return 1 else return 0
def createPosition():
try:
result=api.placeOrder('qty'=100, 'action'='buy')
if result['status']=='OK':
is_in_position=1
except:
print("An Exception has happened (1)")
def checkPositionsForChanges():
# Check Positions, exit if the move is against you
is_exit=isExit()
if is_exit:
try:
result=api.placeOrder('qty'=100, 'action'='sell')
if result['status']=='OK':
is_in_position=2
except:
print("An Exception has happened (2)")
# THE MAIN WHILE LOOP
while True:
if is_in_position==0:
createPosition()
is_in_position=1
elif is_in_position==1:
checkPositionsForChange()
elif is_in_position==2:
break
time.sleep(1)
The problem is coming from
result=api.placeOrder('qty'=100, 'action'='buy')
api.placeOrder is a broker SDK function, it doesn't throw any exception, not even timeout error, and I have no control over that.
How do I work around it, such that the non-response from the broker doesn't stall me
英文:
I have written code to receive Live market data using Websockets, I analyze the data from within the main Loop (with a sleep time of 0.1 seconds between iterations) and take trading decisions based on the current market levels, from within the main loop I also print the stats every 10 seconds or so. Entire code gets executed within 0.001 seconds (except in some rare cases), so each loop takes about 0.101 seconds (thought these numbers might be helpful). The code runs for 375 minutes a day, I mostly use Visual Code Studio to Write/ Edit and Run code, on Windows 10 64 bit.
The problem I'm facing is that the execution freezes sometimes, The code doesn't print updates, It doesn't execute anything at all, goes comatose. I can't get out of it even by pressing CTRL+C. If the problem was because of the command getting lost in some corner of the code in an eternal loop, we should have been able to exit it by pressing CTRL+C right? At first I thought this is Visual Studio Code problem, the same happens when I try to Run the code through IDLE
Seems like some basic Python issue, which the knowledgeable coders might be able to pinpoint, Please help me find the bug. Thanks in advance
I tried steps to speed up Visual studio Code, uninstalled extensions that I don't need. But it doesn't look like VSC problem. I'm out of ideas
EDIT:
I have come to the understanding that the problem is most likely happening because of an http request to the broker that doesn't return an acknowledgement (The broker confirmed that the order would be in eternal wait without either a rejection or completion)
So here is what the sample code looks like:
import time
import api
is_in_position=0
def isExit():
# If exit is necessary return 1 else return 0
def createPosition():
try:
result=api.placeOrder('qty'=100, 'action'='buy')
if result['status']=='OK':
is_in_position=1
except:
print("An Exception has happened (1)")
def checkPositionsForChanges():
# Check Positions, exit if the move is against you
is_exit=isExit()
if is_exit:
try:
result=api.placeOrder('qty'=100, 'action'='sell')
if result['status']=='OK':
is_in_position=2
except:
print("An Exception has happened (2)")
# THE MAIN WHILE LOOP
while True:
if is_in_position==0:
createPosition()
is_in_position=1
elif is_in_position==1:
checkPositionsForChange()
elif is_in_position==2:
break
time.sleep(1)
The problem is coming from
result=api.placeOrder('qty'=100, 'action'='buy')
api.placeOrder is a broker SDK function, it doesn't throw any exception, not even timeout error, and I have no control over that.
How do I work around it, such that the non-response from the broker doesn't stall me
答案1
得分: 1
你提到的问题似乎是代码进入了一个无法退出的循环。Ctrl+C命令在当前命令完成后立即执行,但如果这个命令是一个非常大的计算(或者可能会超时的API调用),它可能不起作用,您可能需要强制停止VSCode的执行。
在没有查看代码的情况下,要找到确切的问题有点困难,但我可以尝试在接收到股票数据时添加一个超时。
希望这有所帮助!
英文:
As you mention, your problem seems to be that the code is entering a loop from which it can't exit. The Ctrl+C command is executed immediately after the current command finishes, but if this command is an extremely large calculation (or an API call that might time out), it wouldn't work and you would need to force the stop of the execution of VSCode.
Without taking a look at the code, is a bit difficult to find the exact problem, but I might try to add a timeout when receiving your stock data.
Hope that this help!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论