1

我正在尝试使用云发布/订阅从主题中提取消息数据,但我每次都收到 ImportError 说明

ImportError: dynamic module does not define init function (init_psutil_linux)

这是我的环境信息:

  • App Engine 标准环境
  • Python2.7

这是我的代码:

import logging
import json
# from datetime import datetime, timedelta
import time

# Imports the Google Cloud client library
from google.cloud import pubsub_v1

import webapp2


class PullMessageData(webapp2.RequestHandler):
    """Pull Pub/Sub Message Data."""

    def get(self):
        self.response.headers['Content-Type'] = 'application/json'

        try:
            subscriber_name = 'subscriber_name'

            # pull subscription message
            pull_message_data(subscriber_name)
            self.response.write(json.dumps({'status': 'OK'}))

        except Exception, e:
            logging.exception(str(e))
            self.response.write(json.dumps({'status': 'ERROR'}))


def pull_message_data(subscriber_name):
    """pull message data from pubsub."""

    # instantiate the pubsub client for a default credential
    subscriber = pubsub_v1.SubscriberClient()

    # callback function for processing the Message Data
    def callback(message):           

        # process data
        print('Received message: {}'.format(message.data))

        # ack the message after processing
        message.ack()

    # Limit the subscriber to only have 5000 outstanding messages at a time.
    flow_control = pubsub_v1.types.FlowControl(max_messages=3)
    # pull message
    subscriber.subscribe(
        subscriber_name, callback=callback, flow_control=flow_control)

    # 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(subscriber_name))
    while True:
        time.sleep(10)

我遵循了来自google cloud pubsub 文档页面的文档 但是在我部署之后,我不断收到一个 ImportError ,上面写着:

(/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/runtime/wsgi.py:263)
Traceback (most recent call last):
  File "/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/base/data/home/apps/b~ups/1.404014226174083542/main.py", line 4, in <module>
    import pull_data
  File "/base/data/home/apps/b~ups/1.404014226174083542/pull_data.py", line 9, in <module>
    from google.cloud import pubsub_v1
  File "/base/data/home/apps/b~ups/1.404014226174083542/libs/google/cloud/pubsub_v1/__init__.py", line 17, in <module>
    from google.cloud.pubsub_v1 import types
  File "/base/data/home/apps/b~ups/1.404014226174083542/libs/google/cloud/pubsub_v1/types.py", line 20, in <module>
    import psutil
  File "/base/data/home/apps/b~ups/1.404014226174083542/libs/psutil/__init__.py", line 91, in <module>
    from . import _pslinux as _psplatform
  File "/base/data/home/apps/b~ups/1.404014226174083542/libs/psutil/_pslinux.py", line 26, in <module>
    from . import _psutil_linux as cext
ImportError: dynamic module does not define init function (init_psutil_linux)

这是我从以下位置调用文件的地方:

import os
import webapp2
import pull_data


app = webapp2.WSGIApplication([
    ('/pull', pull_data.PullMessageData),
], debug=True)

请有人能帮忙弄清楚什么是错的......

4

0 回答 0