Programming Model : Basics
See original GitHub issueScript File
Python support for Functions will follow the existing scripting model and support a run file of .py format. We’ll use the workflow below to determine which file to run:
If the “scriptFile” attribute is set in function.json Else if the project contains a single .py file Else if main.py is present Else throw an error
Entry Point
Instead of wrapping everything in a class, we’ll define a main() method for the runtime to invoke during an execution. We’ll use the workflow below to determine which function to run:
If the “entryPoint” attribute is set in function.json Else if main() is present Else throw an error
Function format
Any references to external modules will be included at the beginning of the file using the import keyword. Additional classes and/or helper functions can also be added to the same file.
# main.py
import os
def main():
pass
Since only a single Python process can exist per function app, it is recommended to implement main() as an asynchronous coroutine using the async def statement.
# Would be run with asyncio directly
async def main():
await some_nonblocking_socket_io_op()
If the main()
function is synchronous (no async
qualifier) we automatically run it in an asyncio thread-pool:
# Would be run in an asyncio thread-pool
def main():
some_blocking_socket_io()
This approach gives the user a choice of what libraries they want to use and async/await becomes an opt-in.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:7 (2 by maintainers)
Top GitHub Comments
“Else if the script contains a single function” —this can be problematic. It’s hard to detect if a module defines a function or if it just imports it. We would need to parse the file and analyze the AST to implement this.
Instead, I suggest to shorten this to:
main()
is presentFrom the offline discussion, we’ll adopt
__init__.py
as the default script file for Python.Let’s update the docs - https://pythondeveloperguide.azurewebsites.net/before closing the issue.