<jupyter_start><jupyter_text>Simple Hello World example for IBM Cloud Functions PyWren This is a simple Hello World example, showing how to take a function and run it with pywren. First we import the necessary libraries to run our functions.<jupyter_code>import numpy as np
import os<jupyter_output><empty_output><jupyter_text>It is possible to use pywren_ibm_cloud inside IBM Watson Studio or Jupyter notebooks in order to run your workloads. You must ensure that the IBM-PyWren package is installed in the environment you are using the notebook. To do so, if you can't install the package manually, we recommend to add the next lines:<jupyter_code>import sys
try:
    import pywren_ibm_cloud as pywren
except:
    !{sys.executable} -m pip install pywren-ibm-cloud
    import pywren_ibm_cloud as pywren<jupyter_output><empty_output><jupyter_text>Installation supports PyWren version as an input parameter, for example:<jupyter_code># !{sys.executable} -m pip install -U pywren-ibm-cloud==1.3.0<jupyter_output><empty_output><jupyter_text>Pywren is designed to run any existing python functions you have, in parallel, at scale, on the cloud. So first, we create an example python function.<jupyter_code>def my_function(x):
    return x + 7<jupyter_output><empty_output><jupyter_text>PyWren needs the configuration to access to IBM Cloud Object Storage and IBM Cloud Functions services. If you don't have the config file in ~/.pywren/config, provide the configuration as a dictionary:<jupyter_code>config = {'ibm_cf':  {'endpoint': '<IBM Cloud Functions Endpoint>', 
                      'namespace': '<NAMESPACE>', 
                      'api_key': '<API KEY>'}, 
          'ibm_cos': {'endpoint': '<IBM COS Endpoint>', 
                      'private_endpoint': '<IBM COS Private Endpoint>', 
                      'api_key' : '<API KEY>'},
          'pywren' : {'storage_bucket' : '<IBM COS BUCKET>'}}<jupyter_output><empty_output><jupyter_text>To start using `pywren`, we first create an executor with the previous config.<jupyter_code>pw = pywren.ibm_cf_executor(config=config)<jupyter_output><empty_output><jupyter_text>We can call `my_function(3)` remotely via `call_async`:<jupyter_code>pw.call_async(my_function, 3)<jupyter_output><empty_output><jupyter_text>Future is a placeholder for the returned value from applying `my_function` to the number `3`. We can call `result` on it and get the result. Note that this will block until the remote job has completed. Once finished it calls `close` to clean all the unnecessary data stored in COS.<jupyter_code>print(pw.get_result())
pw.clean()<jupyter_output><empty_output><jupyter_text>You can apply `my_function` to a list of arguments, and each will be executed remotely at the same time.<jupyter_code>pw = pywren.ibm_cf_executor(config=config)
pw.map(my_function, range(10))<jupyter_output><empty_output><jupyter_text>The pywren `get_all_results` function will wait until all of the futures are done and return their results<jupyter_code>print(pw.get_result())
pw.clean()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08894702419882276, 'fraction_numerical': 0.0029431000654022237, 'mean_word_length': 5.1923076923076925, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '14424', 'license': 'Apache-2.0', 'n_tokens_mistral': 963, 'n_tokens_neox': 937, 'n_words': 321, 'path': 'examples/hello_world.ipynb', 'repo_name': 'tomwhite/pywren-ibm-cloud', 'size': '3058'}