Replies: 2 comments
|
I'm experiencing the same issue. Environment
세부정보
using Python.Runtime;
Runtime.PythonDLL = "C:\\Users\\[username]\\AppData\\Local\\Programs\\Python\\Python312\\python312.dll";
nint threadState = 0;
if(!PythonEngine.IsInitialized)
{
PythonEngine.Initialize();
threadState = PythonEngine.BeginAllowThreads();
PythonEngine.PythonPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
}
//var file = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\run.py";
Parallel.ForEach(Enumerable.Range(1, 3), (i, token) =>
{
using (var gil = Py.GIL())
{
using (var scope = Py.CreateScope())
{
dynamic func = scope.Import("run");
dynamic result = func.text_to_image($"{Guid.NewGuid().ToString()}", $"{Guid.NewGuid().ToString()}.png", "./fonts/NanumGothic.ttf");
}
}
});
//string code = File.ReadAllText("run.py");
//var scriptCompiled = PythonEngine.Compile(code, file);
//scope.Execute(scriptCompiled);
PythonEngine.EndAllowThreads(threadState);
Environment.Exit(0); |
0 replies
|
I solved that problem. The solved code is shown below. using Python.Runtime;
AppContext.SetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", true);
Runtime.PythonDLL = "C:\\Users\\[username]\\AppData\\Local\\Programs\\Python\\Python312\\python312.dll";
nint threadState = 0;
if (!PythonEngine.IsInitialized)
{
PythonEngine.Initialize();
threadState = PythonEngine.BeginAllowThreads();
PythonEngine.PythonPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
}
//var file = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\run.py";
Parallel.ForEach(Enumerable.Range(1, 3), (i, token) =>
{
using (var gil = Py.GIL())
{
using (var scope = Py.CreateScope())
{
dynamic func = scope.Import("run");
dynamic result = func.text_to_image($"{Guid.NewGuid().ToString()}", $"{Guid.NewGuid().ToString()}.png", "./fonts/NanumGothic.ttf");
scope.Dispose();
}
gil.Dispose();
}
});
//string code = File.ReadAllText("run.py");
//var scriptCompiled = PythonEngine.Compile(code, file);
//scope.Execute(scriptCompiled);
PythonEngine.EndAllowThreads(threadState);
PythonEngine.Shutdown();
AppContext.SetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", false); |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Environment
Details
when run above code , console app will hang , never exit , remove code about pythonnet, will exit success
All reactions