I created a NEt web application which does call a python function from C# via pythonnet (v 3.0.5) I can't figureout why it crashes. Any help will be appreciated.
private async Task<string> RunDocling()
{
try
{
// Load the PDF file as byte array
var pdfBytes = await ReadFileAsync($"{basePath}\\{fileName}.pdf");
PythonEngine.Initialize();
using (Py.GIL()) // Acquire Global Interpreter Lock
{
string projectDir = AppDomain.CurrentDomain.BaseDirectory;
string scriptDir = Path.Combine(projectDir, "PythonScripts");
dynamic sys = Py.Import("sys");
sys.path.append(scriptDir);
// Import the Python script
dynamic pyModule = Py.Import("docling_converter");
// Call the function and get the result
string markdownContent = string.Empty;
markdownContent = pyModule.convert_pdf_to_markdown(pdfBytes).ToString();
// Save the output markdown file
await File.WriteAllTextAsync($"{basePath}\\{fileName}.md", markdownContent);
} //CRASH HERE!!!
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
return string.Empty;
}
However, the execution is successful coz I get the result I expect from the Python code. It just not coming back to the .NET code. There is a crash on the end brace of the using
statement. (See the code for the crash point)
No exceptions were thrown.
I get the following error in output windows.
The program '[28220] AiSummaryTest.Web.exe' has exited with code 3221226505 (0xc0000409).
I created a NEt web application which does call a python function from C# via pythonnet (v 3.0.5) I can't figureout why it crashes. Any help will be appreciated.
private async Task<string> RunDocling()
{
try
{
// Load the PDF file as byte array
var pdfBytes = await ReadFileAsync($"{basePath}\\{fileName}.pdf");
PythonEngine.Initialize();
using (Py.GIL()) // Acquire Global Interpreter Lock
{
string projectDir = AppDomain.CurrentDomain.BaseDirectory;
string scriptDir = Path.Combine(projectDir, "PythonScripts");
dynamic sys = Py.Import("sys");
sys.path.append(scriptDir);
// Import the Python script
dynamic pyModule = Py.Import("docling_converter");
// Call the function and get the result
string markdownContent = string.Empty;
markdownContent = pyModule.convert_pdf_to_markdown(pdfBytes).ToString();
// Save the output markdown file
await File.WriteAllTextAsync($"{basePath}\\{fileName}.md", markdownContent);
} //CRASH HERE!!!
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
return string.Empty;
}
However, the execution is successful coz I get the result I expect from the Python code. It just not coming back to the .NET code. There is a crash on the end brace of the using
statement. (See the code for the crash point)
No exceptions were thrown.
I get the following error in output windows.
Share Improve this question asked Mar 26 at 8:13 SurenSalukaSurenSaluka 1,5913 gold badges19 silver badges38 bronze badges 6 | Show 1 more commentThe program '[28220] AiSummaryTest.Web.exe' has exited with code 3221226505 (0xc0000409).
1 Answer
Reset to default 1You can not take GIL in one thread and release in another (which is what happens when you await
before leaving using
block).
Essentially the code inside Py.GIL
can not have await
s unless you are in an unlikely situation with a single threaded scheduler.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744158862a4560995.html
Console.WriteLine
. Don't try to hide exceptions either. As you found out, that won't prevent crashes. Access violation errors for example will crash the application, because the OS can't trust the app's code or data any more. You can't hide that. – Panagiotis Kanavos Commented Mar 26 at 9:030xc0000409
is a stack buffer overrun error thrown by the OS itself. It means the combination of Python.NEt, docling and your own custom script is NOT working. We can't guess whatdocling_converter
andconvert_pdf_to_markdown
are doing or even whatconvert_pdf_to_markdown
returns. Why is a call toToString()
made? Doesconvert_pdf_to_markdown
try to return a char array on the stack that's too large? – Panagiotis Kanavos Commented Mar 26 at 9:19dynamic
. – Panagiotis Kanavos Commented Mar 26 at 9:220xc0000409
error. – Panagiotis Kanavos Commented Mar 26 at 9:24