4

I am learning to use Azure functions. So, I might sound stupid. I am writing a timer trigger function which runs every 1 minute and adds two number and writes to a file. This works fine since I can write the output to the file on the local server.

As a second step, I wanted to write the output to the blob. Below is the code:

import datetime
import logging  
import azure.functions as func

a=4
b=5
sum=a+b
file_name= open("sum.txt","w+")


def main(mytimer: func.TimerRequest, outputBlob: func.Out[str]) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()
    if mytimer.past_due:
        global sum
        global file_name
        print("sum:", sum)
        logging.info('The sum has been calculated!')



    logging.info(sum)
    logging.info('Python timer trigger function ran at %s', utc_timestamp)
    with open("sum.txt", "a") as file_name:
        file_name.seek(0)
        file_name.write("\n")
        file_name.write("Sum: %s" % sum)

    outputBlob.set(file_name)

However, when I run the function I get the below error:

[10/02/2020 14:06:00] Executed 'Functions.CalcPayment' (Failed, Id=547f7a3d-03b4-4a02-98e7-f4bfb73e6f5e)
[10/02/2020 14:06:00] System.Private.CoreLib: Exception while executing function: Functions.CalcPayment. System.Private.CoreLib: Result: Failure
[10/02/2020 14:06:00] Exception: TypeError: unable to encode outgoing TypedData: unsupported type "<class 'azure_functions_worker.bindings.generic.GenericBinding'>" for Python type "int"
[10/02/2020 14:06:00] Stack:   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.2106/workers/python/3.7/OSX/X64/azure_functions_worker/dispatcher.py", line 330, in _handle__invocation_request
[10/02/2020 14:06:00]     pytype=out_type_info.pytype)
[10/02/2020 14:06:00]   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.2106/workers/python/3.7/OSX/X64/azure_functions_worker/bindings/meta.py", line 83, in to_outgoing_proto
[10/02/2020 14:06:00]     f'unable to encode outgoing TypedData: '
[10/02/2020 14:06:00] .
2
  • 1
    Would you mind fixing your indentation? Commented Feb 10, 2020 at 14:29
  • Does that help? Commented Feb 10, 2020 at 14:39

2 Answers 2

6

you dont need to have a file to write to, just create a string that makes sense in your case and push it to the output variable. You can use this sample as a starting point.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks !! yes this idea also worked. Well, writing to the local file initially helped me to verify if the function is working. Later on, removed that bit and wrote directly to a Blob. Do you know if we can append to the blob ?
0

Ok. So the issue was very simple. I had to convert the sum to string and write to the blob and everything worked. Thanks !!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.