【发布时间】:2019-06-14 10:21:53
【问题描述】:
从previous thread 的开发发现,提出问题时的假设是题外话(子流程实际上并没有导致问题),所以我正在做一个更有针对性的帖子。
我的错误信息:
找不到记录器的处理程序 “google.cloud.pubsub_v1.subscriber._protocol.streaming_pull_manager”
我的意图:
将 Google PubSub 消息属性作为 Python 变量传递,以便在以后的代码中重复使用。
我的代码:
import time
import logging
from google.cloud import pubsub_v1
project_id = "redacted"
subscription_name = "redacted"
def receive_messages_with_custom_attributes(project_id, subscription_name):
"""Receives messages from a pull subscription."""
# [START pubsub_subscriber_sync_pull_custom_attributes]
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(
project_id, subscription_name)
def callback(message):
print('Received message: {}'.format(message.data))
if message.attributes:
#print('Attributes:')
for key in message.attributes:
value = message.attributes.get(key);
#commented out to not print to terminal
#which should not be necessary
#print('{}: {}'.format(key, value))
message.ack()
print("this is before variables")
dirpath = "~/subfolder1/"
print(dirpath)
namepath = message.data["name"]
print(namepath)
fullpath = dirpath + namepath
print(fullpath)
print("this is after variables")
subscriber.subscribe(subscription_path, callback=callback)
# The subscriber is non-blocking, so we must keep the main thread from
# exiting to allow it to process messages in the background.
print('Listening for messages on {}'.format(subscription_path))
while True:
time.sleep(60)
# [END pubsub_subscriber_sync_pull_custom_attributes]
receive_messages_with_custom_attributes(project_id, subscription_name)
我运行上述代码后的完整控制台输出:
Listening for messages on projects/[redacted]
Received message: {
"kind": "storage#object",
"id": "[redacted]/0.testing/1548033442364022",
"selfLink": "https://www.googleapis.com/storage/v1/b/[redacted]/o/BSD%2F0.testing",
"name": "BSD/0.testing",
"bucket": "[redacted]",
"generation": "1548033442364022",
"metageneration": "1",
"contentType": "application/octet-stream",
"timeCreated": "2019-01-21T01:17:22.363Z",
"updated": "2019-01-21T01:17:22.363Z",
"storageClass": "MULTI_REGIONAL",
"timeStorageClassUpdated": "2019-01-21T01:17:22.363Z",
"size": "0",
"md5Hash": "1B2M2Y8AsgTpgAmY7PhCfg==",
"mediaLink": "https://www.googleapis.com/download/storage/v1/b/[redacted]/o/BSD%2F0.testing?generation=1548033442364022&alt=media",
"crc32c": "AAAAAA==",
"etag": "CPb0uvvZ/d8CEAE="
}
this is before variables
/home/[redacted]
No handlers could be found for logger "google.cloud.pubsub_v1.subscriber._protocol.streaming_pull_manager"
如您所见,第一个字符串和 string-defined-as-variable 已打印,但代码在尝试从刚刚生成的字典中定义变量时中断,并且没有进一步执行 print()s。
Potentially related thread,该用户使用 cron 作业发布,并从 crontab envpaths 中找到了修复程序,但我的情况是接收但未使用任何 cron 作业,但可能暗示 python 后面/内部的另一层?
谁能帮我添加一个处理程序以使此代码按预期运行?
【问题讨论】:
-
那么你在运行这个时是否设置了
GOOGLE_APPLICATION_CREDENTIALS? -
它在谷歌云引擎上运行,所以我只去了
gcloud auth login。够了吗,还是我需要下载和export.json 密钥?
标签: python logging google-cloud-platform handler google-cloud-pubsub