Workspace 6.21.5
|
As Python is deployed with Workspace we can leverage the functionality of Python from within our workflows. In this tutorial we will use features from two of the standard Python modules (threading and urllib) to fetch a couple of web pages in parallel from within a RunPythonScript Operation.
In this brief tutorial we will use the RunPythonScript Operation. Just in case you get stuck, a sample workflow has been provided for you.
from threading import Thread from urllib.request import urlopen # On Linux and OSX, the certificate file (cafile) in OpenSSL was consolidated # at build time, this leaves Python modules like ssl not able to access the # cafile. To workaround it, on Linux and OSX, the module certifi has been # pre-installed to help find the correct cafile. Simply pass # cafile=certifi.where() # to the urlopen() function and it should work properly. To make the script # run on Windows without certifi installed, you can use a try block like this: try: import certifi cafile = certifi.where() except ImportError: cafile = None class URLThread(Thread): response = "" url = "" def __init__(self,_url): Thread.__init__(self) self.url=_url def run(self): with urlopen(self.url, cafile=cafile) as response: self.response = response.read().decode('utf-8') thread1 = URLThread("http://docs.python.org/3/library/threading.html") thread2 = URLThread("http://docs.python.org/3/howto/urllib2.html") thread1.start() thread2.start() thread1.join() thread2.join() outputA=thread1.response outputB=thread2.response
You can add further Python modules using the included pip.
Although a brief tutorial it does demonstrate that the power of the standard Python modules can be leveraged within your workflows to perform a wide range of tasks.