gpt4 book ai didi

python - 从 redis 队列到 azure 容器实例的超时错误

转载 作者:行者123 更新时间:2023-12-02 19:29:44 25 4
gpt4 key购买 nike

我有几个应用程序部署到 azure 容器实例中,我通过 Flask API 访问这些实例,现在我正在创建一个处理程序来启动/停止容器,例如

app1位于container1中,如果我收到对app1的请求,我的工作流程是:

1) Start the contianer1 from a python subprocess using azure CLI comands 
2) Make the API call to app1
3) Get the result and stop the container

由于我一次可以有多个请求,因此我将该过程处理到一个函数中,添加到 Redis 队列中,如下所示:

def make_call(arg1,arg2,arg3):


start_container = 'az container start --name mycontainer --resource-group mygroup'
subprocess.call([start_container],shell=True)



data = dict()
data['query'] = {'query':"query"}
####


url = "http://api_url"
resp = requests.post(url,data=json.dumps(data), headers=headers )
print (resp)
stop_container = 'az container stop --name mycontainer --resource-group mygroup'
subprocess.call([stop_container],shell=True)

return resp

因此,每次收到请求时,都会将一个任务添加到队列中:

r = redis.Redis()
q = Queue(connection=r,default_timeout=3600)
task = q.enqueue(make_call,args = (arg1,arg2,arg3),timeout=500)
print (q.jobs)

如果我只进行一次调用,它就可以正常工作,并且我可以从 azure 中看到容器是如何打开和关闭的:

enter image description here

但是当然,这不是队列的目的,当我向队列添加多个作业时,第一个作业正常,但其余作业失败:

Traceback (most recent call last):
File "/home/luis/anaconda3/lib/python3.7/site-packages/urllib3/connection.py", line 159, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw)
File "/home/luis/anaconda3/lib/python3.7/site-packages/urllib3/util/connection.py", line 80, in create_connection
raise err
File "/home/luis/anaconda3/lib/python3.7/site-packages/urllib3/util/connection.py", line 70, in create_connection
sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out

此外,在这种情况下我不会使用 kubernetes,因为我只有少数服务每周会使用一到两次。

**编辑:**

我已经发现了错误,但还不知道如何解决。如果我删除启动和停止容器的行,我工作得很好,所以问题似乎与我开始发出请求时容器尚未准备好有关,有什么方法可以确保容器启动并运行在进行 API 调用之前?

编辑2

目前我正在使用 CLI 创建容器喜欢:

az container create -g MyResourceGroup --name myapp --image myimage:latest --cpu 1 --memory 1

是否可以从那里开始,或者我是否必须将其全部更改为 YML 文件?

最佳答案

您可以配置liveness probe为此,当您创建容器时,当它启动时,它会等待 active 探针返回成功,然后再将流量转发到容器。

apiVersion: 2018-06-01
location: eastus
name: livenesstest
properties:
containers:
- name: mycontainer
properties:
image: nginx
ports: []
resources:
requests:
cpu: 1.0
memoryInGB: 1.5
livenessProbe:
exec:
command:
- "curl"
- "http://localhost"
periodSeconds: 5
osType: Linux
restartPolicy: Always
type: Microsoft.ContainerInstance/containerGroups

类似的事情

关于python - 从 redis 队列到 azure 容器实例的超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58212569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com