0

I have an existing .NET Core / asp.net app service hosted on Azure. I need to call (on demand) a python script to return data based on custom user input.

It does not appear that I can use IronPython, since I need python modules that are built in CPython, which unfortunately aren't supported by IronPython.

The two options I see are:

What is the best way to call a python script on demand from .NET app service on Azure?

John
  • 1
  • 2
  • If python script is simple, you can consider azure automation [python runbook](https://learn.microsoft.com/en-us/azure/automation/automation-first-runbook-textual-python2). – Ivan Glasenberg Jan 02 '19 at 06:15
  • I'll look into that. While the script is simple, the libraries it needs to use are definitely NOT simple. If it can't import advanced libraries, it won't work. That's the same reason I can't use IronPython. – John Jan 02 '19 at 06:52
  • Ok, if any problem during the runbook, you can let me know. good luck:) – Ivan Glasenberg Jan 02 '19 at 06:59
  • It looks like that is really only for Azure automation, which isn't the problem I am trying to solve, and wouldn't work as an on demand service (SaaS) anyway. – John Jan 02 '19 at 12:56

1 Answers1

2

Per my experience, there are two ways to call a Python script in C# without IronPython.

  1. Directly use System.Diagnostics.Process in C# to run a command as same as the SO thread Run Command Prompt Commands to get the result via parse the content of the process standard output. Simply to do it, you can use py2exe to wrap a Python script as a .exe file to avoid for installing Python modules and setting environment variables on Azure App Service. However, considering for concurrency, it's not a good idea for performance.

  2. The second option as you said is to deploy a Python script as a REST API in the same instance of Azure Web App. You can follow the blog Deploying multiple virtual directories to a single Azure Website to deploy a flask app with your Python script as a child project via Visual Studio with PTVS to expose an API url like https://<your web app name>.azurewebsites.net/pyapi which can be called from your ASP.NET via HttpClient. I tried this solution, it works.

Note: Due to the restriction of Azure Web App sandbox for Local Address Requests, you have to use <your web app name>.azurewebsites.net as hostname, neither localhost or 127.0.0.1.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43