question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Programming Model : Basics

See original GitHub issue

Script 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
1st1commented, Mar 5, 2018

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 the script contains a single function Else if main() is present Else throw an error

“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:

  • If the “entryPoint” attribute is set in function.json
  • Else if main() is present
  • Else throw an error
0reactions
asavaritayalcommented, Mar 21, 2018

From 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

1.1 Programming Model - Algorithms, 4th Edition
Basic programming model. A library of static methods is a set of static methods that are defined in a Java class. A basic...
Read more >
Programming model - Wikipedia
A programming model is an execution model coupled to an API or a particular pattern of code. In this style, there are actually...
Read more >
23 Model basics | R for Data Science
23.1 Introduction The goal of a model is to provide a simple low-dimensional summary of a dataset. In the context of this book...
Read more >
Linear programming basics
So a linear programming model consists of one objective which is a linear equation that must be maximized or minimized. Then there are...
Read more >
Math Programming Modeling Basics - Gurobi Optimization
Mathematical modeling has a number of commonly occurring model patterns that appear in many different applications. Examples include fixed-charge network flow, ...
Read more >

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found