[英]Google Cloud PubSub not being called properly in Python
I am squeezing my brain but I am not getting why this issue is happening and I couldnt figure out the cause. 我挤我的大脑,但我不明白为什么这个问题正在发生,我无法找出原因。 I am trying to read an image and pass it to pubsub.
我正在尝试读取图像并将其传递给pubsub。 Once the messages are sent through pubsub, it is redirected to AutoML model to identify or predict the given image.
通过pubsub发送消息后,会将其重定向到AutoML模型以识别或预测给定图像。 Below is the code snippet
下面是代码片段
global val1
@app.route("/", methods=['GET', 'POST'])
doc_type=request.form.get('submit_button')
file_name = secure_filename(file.filename)
blob=file.read()
flash('File upload successful', 'info')
# Initializing PubSub
publisher,subscriber,topic,subscription=pubsub_init(doc_type)
blob=blob+bytes(doc_type,'utf-8')
subscriber.subscribe(subscription,callback)
publisher.publish(topic,blob)
flash("the uploaded file is "+val1,'info')
Init function: 初始化功能:
def pubsub_init(doctype):
publisher=pubsub.PublisherClient()
subscriber=pubsub.SubscriberClient()
if doctype=="License":
subscription=<<sub name>>
elif doctype=="Credit":
subscription=<<subname>>
elif doctype=="Passport":
subscription=<<subname>>
else:
print("invalid choice"
topic=<<topic>>
print(subscription)
return (publisher,subscriber,topic,subscription)
My Callback: 我的回电:
def callback(message):
#print("hello",flush=True)
print("making global")
project_id=<<proj id>>
val1,val2=predict_value(new_message,model_id,project_id,compute_region)
message.ack()
But I am getting error like val1 not defined. 但是我收到了类似val1的错误,但未定义。 Could you please advice on this?
您能对此提出建议吗?
The issue here is that subscriber.subscribe(subscription, callback)
is setting up an asynchronous call to callback
. 这里的问题是
subscriber.subscribe(subscription, callback)
正在设置对callback
的异步调用。
This means that when you publish a new topic, you're essentially setting up a race condition between whether the call to flash(...)
will get executed first, or the callback. 这意味着在发布新主题时,实际上是在设置是否首先执行对
flash(...)
的调用或回调之间的竞争条件。 Since the callback is presumably taking some time to complete, the flash
line is winning, but val1
hasn't been created yet, hence your error. 由于回调可能要花一些时间才能完成,所以
flash
行很成功,但是尚未创建val1
,因此会出现错误。
There are ways to control the concurrency which might make what you're trying to do possible by blocking on the subscriber's future. 有多种控制并发的方法,这些方法可能会阻止订阅者的未来,从而使您尝试执行的操作成为可能。
However before attempting that, I would ask why you're trying to use pub/sub here in the first place. 但是,在尝试该操作之前,我会问您为什么首先尝试在此处使用pub / sub。 It seems like you're just setting up a publisher and subscriber to publish a single message, and then trying to do something with the result of that message.
似乎您只是在设置发布者和订阅者来发布一条消息,然后尝试对该消息的结果进行处理。 Why not just do it all inline?
为什么不全部全部内联?
@app.route("/", methods=['GET', 'POST'])
def your_function(request):
doc_type=request.form.get('submit_button')
file_name = secure_filename(file.filename)
blob=file.read()
flash('File upload successful', 'info')
blob=blob+bytes(doc_type,'utf-8')
# turn a blob into new_message, get model_id from somewhere?
project_id=<<proj id>>
val1,val2=predict_value(new_message,model_id,project_id,compute_region)
flash("the uploaded file is "+val1,'info')
If you're going to call a global, you have to declare as such in the function: 如果要调用全局变量,则必须在函数中声明为:
def callback(message):
global val1
global val2
...
val1, val2 = predict_value(new_message,model_id,project_id,compute_region)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.