Giter VIP home page Giter VIP logo

Comments (13)

henon avatar henon commented on September 15, 2024 1

And this is of course still necessary in Global.asax.cs

            // this call initializes numpy. it is necessary to do that before PythonEngine.BeginAllowThreads()
            np.arange(1);
            PythonEngine.BeginAllowThreads(); // <--- this is very important for a web server since all requests are on different threads

from numpy.net.

Esther2013 avatar Esther2013 commented on September 15, 2024
    public class Program
    {
        public static void Main(string[] args)
        {
            PythonEngine.Initialize();
            PythonEngine.BeginAllowThreads();

            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }

Add these two lines:

            PythonEngine.Initialize();
            PythonEngine.BeginAllowThreads();

from numpy.net.

normalclone avatar normalclone commented on September 15, 2024
    public class Program
    {
        public static void Main(string[] args)
        {
            PythonEngine.Initialize();
            PythonEngine.BeginAllowThreads();

            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }

Add these two lines:

            PythonEngine.Initialize();
            PythonEngine.BeginAllowThreads();

@Esther2013
image
I can't find Program.cs. I put it into Application_Start in global but It cause other problem
Unable to load DLL 'python37': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

from numpy.net.

henon avatar henon commented on September 15, 2024

When you look at the Examples folder of the Numpy.NET source, there is a WebApiExample. Can you check it out please and see if that works for you? I am pretty sure it is, like Esther Hu says, that you are missing the line PythonEngine.BeginAllowThreads(); at some point.

from numpy.net.

henon avatar henon commented on September 15, 2024

ok, @Esther2013 was almost correct but not quite. You need to do this:

            // this call initializes numpy. it is necessary to do that before PythonEngine.BeginAllowThreads()
            np.arange(1);
            PythonEngine.BeginAllowThreads(); // <--- this is very important for a web server since all requests are on different threads

from numpy.net.

normalclone avatar normalclone commented on September 15, 2024

Hi @henon! After solving the Dll not found problem in Local IIS by using
Environment.SetEnvironmentVariable("PATH", @"C:\Python37\",EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("PYTHONHOME", @"C:\Python37\", EnvironmentVariableTarget.Process);

and also add Numpy.np.arange(1); PythonEngine.BeginAllowThreads(); in Startup.cs, it cause new problem. When ever I run to this line
var rs = np.linalg.norm(np.array(arr1) - np.array(arr2));
IIS force close without any message except Exception thrown: 'System.AccessViolationException' in Python.Runtime.dll in VS output window
I've tried pass PythonEngine.BeginAllowThreads(); after and before var rs = np.linalg.norm(np.array(arr1) - np.array(arr2)); It still close suddenly.

--update 1: I still try update code to IIS server but a weird err Access to the path 'c:\windows\system32\inetsrv\python-3.7.3-embed-amd64.zip' is denied appear in Numpy.np.arange(1);, then I tried to set permission but seems like setting permission is not allowed.
--update 2: After commented Numpy.np.arange(1);, Unable to load DLL 'python37': The specified module could not be found. (Exception from HRESULT: 0x8007007E) appear again, OMG :((

from numpy.net.

henon avatar henon commented on September 15, 2024

ok, so it seems in your usecase you can't use numpy.net which automatically install python in the AppData directory per default. Can you manually install python on the machine or should it be deployed automatically? Depending on the choice there are different solutions.

if you have manually set up numpy in C:\Python37 you should reference the Numpy.Bare nuget. Which do you reference?

from numpy.net.

normalclone avatar normalclone commented on September 15, 2024

@henon
I've tried manually set up numpy in C:\Python37 and reference Numpy.Bare but PythonEngine.BeginAllowThreads(); still force close IIS.

from numpy.net.

henon avatar henon commented on September 15, 2024

can you create a minimal reproduction project and share it on github?

from numpy.net.

normalclone avatar normalclone commented on September 15, 2024

@henon https://github.com/normarclone/testNumpy/ here! This is a very minimal project. The code is placed in HomeController and Global.asax file

from numpy.net.

henon avatar henon commented on September 15, 2024

I solved it. You need to wrap all code that accesses numpy in using(Py.GIL()) { ... } in the controller:

    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            //Get 2 sample arrays
            double[] Feature1 = JsonConvert.DeserializeObject<List<double>>(Features.Feature1).ToArray();
            double[] Feature2 = JsonConvert.DeserializeObject<List<double>>(Features.Feature2).ToArray();

            using (Py.GIL())
            {
                //Convert it to np.array
                NDarray arr1 = np.array(Feature1);
                NDarray arr2 = np.array(Feature2);
                //Process
                var result = np.linalg.norm(np.array(arr1 - arr2)).ToString();
                //Result : "1.4386139661537802"
                return Json(result, JsonRequestBehavior.AllowGet);
            }
        }
    }

from numpy.net.

henon avatar henon commented on September 15, 2024

In the setup you shared on github you had used my Numpy package, which installs python and numpy in the AppData directory. So installing python in C://python37 is not necessary

from numpy.net.

normalclone avatar normalclone commented on September 15, 2024

Thank you @henon! Issue close here. Thank you very much πŸ’―

from numpy.net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.