I am trying to use the cloud pub/sub to pull message data from a topic but I keep getting an ImportError every time stating that
ImportError: dynamic module does not define init function (init_psutil_linux)
This is my environment info:
- App Engine Standard Environment
- Python2.7
This is my code:
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)
I followed the documentation from the google cloud pubsub documentation page But after I deployed I kept getting an ImportError which says this:
(/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)
This is where I am calling the file from:
import os
import webapp2
import pull_data
app = webapp2.WSGIApplication([
('/pull', pull_data.PullMessageData),
], debug=True)
please can someone help figure what is wrong.....