我正在使用Python3模块:
requests
对于HTTP GET调用几个粒子光子,这些粒子光子被设置为简单的HTTP服务器
作为一个客户端,我使用Raspberry Pi(它也是一个访问点)作为一个HTTP客户机,它使用multiprocessing.dummy.Pool
对上述光子进行HTTP GET响应
轮询程序如下:
def pollURL(url_of_photon):
"""
pollURL: Obtain the IP Address and create a URL for HTTP GET Request
@param: url_of_photon: IP address of the Photon connected to A.P.
"""
create_request = 'http://' + url_of_photon + ':80'
while True:
try:
time.sleep(0.1) # poll every 100ms
response = requests.get(create_request)
if response.status_code == 200:
# if success then dump the data into a temp dump file
with open('temp_data_dump', 'a+') as jFile:
json.dump(response.json(), jFile)
else:
# Currently just break
break
except keyboardInterrupt as e:
print('KeyboardInterrupt detected ', e)
break
url_of_photon
值是从Pi上的dnsmasq.leases
文件获得的简单的IPv4地址。在
main()
函数:
程序运行良好,但是当我按下CTRL+C时,程序不会终止。经过挖掘,我发现这样做的方法是使用CTRL+\
我如何在我的pollURL
函数中使用它来安全地退出程序,即执行poll.join()
,这样就没有剩下的进程了?在
注意事项:
函数永远无法识别KeyboardInterrupt
。因此,我在尝试检测CTRL+\时遇到了麻烦。在
pollURL
在另一个线程中执行。在Python中,信号只在主线程中处理。因此,SIGINT只在主线程中引发键盘中断。在从signal documentation:
您可以用以下方式实现您的解决方案(伪代码)。在
相关问题 更多 >
编程相关推荐