Adding and Using psutil Dependency
Step 1 - Adding psutil dependency
Open requirements.txt and add psutil``==5.9.4 as follows:
requirements.txt
#
# Specify any third-party Python modules that should be bundled with the
# extension. uip-cli will automatically download and bundle the modules
# upon running `uip build` or `uip push`.
#
# Refer to https://pip.pypa.io/en/stable/reference/requirements-file-format/
# for the expected format.
#
requests==2.28.2
psutil==5.9.4
In addition to modifying requirements.txt, we will also need to modify src/extension.ymlas follows:
extension.yml
extension:
name: ue-task
version: "1.0.0"
api_level: "1.4.0"
requires_python: ">=2.6"
zip_safe: false
owner:
name: Stonebranch
organization: Stonebranch Inc.
comments: |
Created using ue-task template
On line 6, the zip_safe property was set to false. This must be done, anytime an Extension uses a third-party dependency requiring the C runtime (e.g. psutil).
Step 2 - Using the psutil dependency
Open extension.py and modify it as follows:
extension.py
from __future__ import (print_function)
from universal_extension import UniversalExtension
from universal_extension import ExtensionResult
from universal_extension import logger
import requests
import psutil
class Extension(UniversalExtension):
"""Required class that serves as the entry point for the extension
"""
def __init__(self):
"""Initializes an instance of the 'Extension' class
"""
# Call the base class initializer
super(Extension, self).__init__()
def extension_start(self, fields):
"""Required method that serves as the starting point for work performed
for a task instance.
Parameters
----------
fields : dict
populated with field values from the associated task instance
launched in the Controller
Returns
-------
ExtensionResult
once the work is done, an instance of ExtensionResult must be
returned. See the documentation for a full list of parameters that
can be passed to the ExtensionResult class constructor
"""
resp = requests.get(
'https://httpbin.org/basic-auth/user/pass',
auth=('user', 'pass')
)
print(resp.status_code)
print(psutil.test())
# Return the result with a payload containing a Hello message...
return ExtensionResult(
unv_output='Hello Extension!'
)
On line 6, we import the psutil module
On lines 43, we print the result of psutil.test()