Re: [Python.NET] PythonDotNet Digest, Vol 170, Issue 2
Thanks Ron, "Did you try background or threadpool threads?" Threading isn't an issue here, everything here is single threaded and synchronous. "Redirected process calls and looking for stdin stdout stderr streams allows more control." Unfortunately, when I do this, I don't get any further information. On the Python side I can tell it loads up to the import statement before the crash, and redirecting the VB output doesn't work as the application has no Console and so no stdout or stderr (also, the Access Violation is treated special in .Net so doesn't get caught, so have to rely on Debuger to get info as below rather than catching and logging info). I guess it comes down to this, in the code for PythonEngine's Shutdown method: "The Python runtime can no longer be used in the current process after calling the Shutdown method." I thought that meant it couldn't be re-used after being Shutdown, but I thought it could be re-used if I ran Initialize again. I guess that is wrong. I was also worried that leaving it hanging around but not Shutdown() would cause issues / count as a memory leak as other than this the rest of the VB application is removed from memory when the session comes to an end. But if it strictly means can't be run in the same process no matter what, I can not call Shutdown() at the end of the VB session and when I start a session I can check if it the PythonEngine is initialized first, and only Initialize() if it hasn't yet been. Follow up question: If I never Shutdown() and the process that running it comes to an end what will happen with the PythonEngine? In my situation I don't know when the process is going to end. I can tell when I am done for a particular session (and so can clean up any PyScopes for example), and this is the time when I was calling Shutdown(). But there can be multiple sessions run from the same process so end of session is not a good a spot to call Shutdown(). Do I risk leaving the PythonEngine or something else in a bad state if I never call Shutdown() and just let the process end? Steve ________________________________ ---------------------------------------------------------------------- Message: 1 Date: Thu, 4 Oct 2018 02:12:14 +0000 (UTC) From: Ron Harding <rharding64@yahoo.com> To: "A list for users and developers of Python for .NET" <pythondotnet@python.org> Subject: Re: [Python.NET] Embedded Python .Net example - running scripts Message-ID: <1651768403.3910584.1538619134927@mail.yahoo.com> Content-Type: text/plain; charset="utf-8" Did you try background or threadpool threads? Redirected process calls and looking for stdin stdout stderr streams allows more control. Sent from Yahoo Mail on Android On Wed, Oct 3, 2018 at 15:47, Luke, Steve<Steve.Luke@moldev.com> wrote: <!--#yiv5337413704 P {margin-top:0;margin-bottom:0;}--> I am getting a crash trying to embed Python into my VB program using Python for .NET.?The goal of my application is to allow users to run their own python scripts inside a larger application.?The crash I am getting happens the second time I run a script that imports certain large libraries (like numpy or scikit-image) but does not affect all libraries (for example PIL). ? My environment: Windows 10 Pro x64, Python 3.6.5 (in Anaconda), Python for .Net 2.4.0 (installed from git?s master branch). ? Here is the VB code: ???PublicFunction Startup(ByRef param AsString)AsInteger ???????PythonEngine.Initialize() ???????'mm.PrintMsg(PythonEngine.BuildInfo) ???????'mm.PrintMsg(PythonEngine.Compiler) ???????'mm.PrintMsg(PythonEngine.Platform) ???????'mm.PrintMsg(PythonEngine.ProgramName) ???????'mm.PrintMsg(PythonEngine.PythonHome) ???????'mm.PrintMsg(PythonEngine.PythonPath) ???????Using (Py.GIL()) ? ???????????Dim scriptPath AsString = param ? ???????????Dim scriptDir AsString = Path.GetDirectoryName(scriptPath) ???????????Dim scriptName AsString = GetPythonModuleName(scriptPath) ???????????Dim scriptParam AsString = "" ? ???????????Dim scriptScope As PyScope = Py.CreateScope() ? ???????????Dim scriptText AsString = My.Computer.FileSystem.ReadAllText(scriptPath) ???????????scriptScope.Exec(scriptText) ? ???????????If scriptScope.Contains(STARTUP_METHOD_NAME) Then ???????????????scriptScope.Exec(String.Format("{0}(r'{1}')", STARTUP_METHOD_NAME, scriptParam)) ???????????EndIf ? ???????????scriptScope.Dispose() ???????EndUsing ???????PythonEngine.Shutdown() ? ???????Return 0 ???EndFunction ? And this is the Python script I am running: import skimage.io ? defStartup(param): ???pass ? defDocommand(param): ???pass ??? defShutdown(): ???pass ? When I run it the VB code the first time, things work great.?When I run the VB code a second time (without first restarting the entire application) I get an access violation exception on the scriptScope.Exec() call: Exception: System.AccessViolationException : Message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Source: Python.Runtime StackTrace: ??at Python.Runtime.Runtime.PyObject_Call(IntPtr pointer, IntPtr args, IntPtr kw) ??at Python.Runtime.ImportHook.__import__(IntPtr self, IntPtr args, IntPtr kw) ??at Python.Runtime.Runtime.PyObject_Call(IntPtr pointer, IntPtr args, IntPtr kw) ??at Python.Runtime.ImportHook.__import__(IntPtr self, IntPtr args, IntPtr kw) ??at Python.Runtime.Runtime.PyObject_Call(IntPtr pointer, IntPtr args, IntPtr kw) ??at Python.Runtime.ImportHook.__import__(IntPtr self, IntPtr args, IntPtr kw) ??at Python.Runtime.Runtime.PyObject_Call(IntPtr pointer, IntPtr args, IntPtr kw) ??at Python.Runtime.ImportHook.__import__(IntPtr self, IntPtr args, IntPtr kw) ??at Python.Runtime.Runtime.PyObject_Call(IntPtr pointer, IntPtr args, IntPtr kw) ??at Python.Runtime.ImportHook.__import__(IntPtr self, IntPtr args, IntPtr kw) ??at Python.Runtime.Runtime.PyObject_Call(IntPtr pointer, IntPtr args, IntPtr kw) ??at Python.Runtime.ImportHook.__import__(IntPtr self, IntPtr args, IntPtr kw) ??at Python.Runtime.Runtime.PyObject_Call(IntPtr pointer, IntPtr args, IntPtr kw) ??at Python.Runtime.ImportHook.__import__(IntPtr self, IntPtr args, IntPtr kw) ??at Python.Runtime.Runtime.PyObject_Call(IntPtr pointer, IntPtr args, IntPtr kw) ??at Python.Runtime.ImportHook.__import__(IntPtr self, IntPtr args, IntPtr kw) ??at Python.Runtime.Runtime.PyObject_Call(IntPtr pointer, IntPtr args, IntPtr kw) ??at Python.Runtime.ImportHook.__import__(IntPtr self, IntPtr args, IntPtr kw) ??at Python.Runtime.Runtime.PyRun_String(String code, IntPtr st, IntPtr globals, IntPtr locals) ??at Python.Runtime.PyScope.Exec(String code, IntPtr _globals, IntPtr _locals) ??at MyApp.MyClass.Startup(String& param) in C:\Git\MyPath\MyApp.vb:line 132 ?Unfortunately the stack trace doesn't show line numbers for anything in the Python.Runtime. ? Interestingly, if you look at the VB code, I have some lines of commented out code: ???????'mm.PrintMsg(PythonEngine.BuildInfo) ???????'mm.PrintMsg(PythonEngine.Compiler) ???????'mm.PrintMsg(PythonEngine.Platform) ???????'mm.PrintMsg(PythonEngine.ProgramName) ???????'mm.PrintMsg(PythonEngine.PythonHome) ???????'mm.PrintMsg(PythonEngine.PythonPath) Since the app doesn?t have a console, this sends the text to the enclosing app for display.?If I uncommon that code, then the first time I run it I get: default, Mar 29 2018, 13:32:41 [MSC v.1900 64 bit (AMD64)] win32 python ? F:\Anaconda3\python36.zip;F:\Anaconda3\Lib;F:\Anaconda3\DLLs;C:\MX6 ? And the second time I run it I get: default, Mar 29 2018, 13:32:41 [MSC v.1900 64 bit (AMD64)] win32 pyth??? ??? F:\Anaconda3\python36.zip;F:\Anaconda3\Lib;F:\Anaconda3\DLLs;C:\MX6 ? And the application doesn?t crash!?Instead it just says the skimage module can?t be found.?Notice the bolded lines (bold added after the fact).?The program name and python home values are being corrupted. Is there something I am doing wrong as far as taking down the PythonEngine such that it could be put back up in an as-new state later on? Steve Please be advised that this email may contain confidential information. If you are not the intended recipient, please notify us by email by replying to the sender and delete this message. The sender disclaims that the content of this email constitutes an offer to enter into, or the acceptance of, any agreement; provided that the foregoing does not invalidate the binding effect of any digital or other electronic reproduction of a manual signature that is included in any attachment._________________________________________________ Python.NET mailing list - PythonDotNet@python.org https://urldefense.proofpoint.com/v2/url?u=https-3A__mail.python.org_mailman_listinfo_pythondotnet&d=DwICAg&c=9mghv0deYPYDGP-W745IEdQLV1kHpn4XJRvR6xMRXtA&r=XBqO7p7APNCVKpdDR1v4BCUJqDo0CwXYsXZCFrPPxqg&m=IBuMQkjHwpGp1pP7cEvq9ZlXMLFp4HtptzWN_0Jl-9w&s=9lviSxlSv0jZM0AEZlYC1llBN_6m6hm-xeEh0BVllyI&e=
participants (1)
-
Luke, Steve