remove allow thread

This commit is contained in:
Jicheng Lu 2025-10-08 15:12:00 -05:00
parent c30787a8ea
commit 2079ba3760
5 changed files with 17 additions and 22 deletions

View file

@ -4,7 +4,7 @@ namespace BotSharp.Abstraction.CodeInterpreter.Models;
public class CodeInterpretOptions
{
public string? ScriptName { get; set; }
public IEnumerable<KeyValue>? Arguments { get; set; }
public bool LockFree { get; set; }
public CancellationToken? CancellationToken { get; set; }
}

View file

@ -13,7 +13,7 @@ public class CodeScriptExecutor
_logger = logger;
}
public async Task<CodeInterpretResult> Execute(Func<Task<CodeInterpretResult>> func, CancellationToken cancellationToken = default)
public async Task<T> Execute<T>(Func<Task<T>> func, CancellationToken cancellationToken = default)
{
await _semLock.WaitAsync(cancellationToken);
@ -24,11 +24,7 @@ public class CodeScriptExecutor
catch (Exception ex)
{
_logger.LogError(ex, $"Error in {nameof(CodeScriptExecutor)}.");
return new CodeInterpretResult
{
Success = false,
ErrorMsg = ex.Message
};
return default(T);
}
finally
{

View file

@ -155,7 +155,7 @@ public partial class InstructService
var db = _services.GetRequiredService<IBotSharpRepository>();
var hooks = _services.GetHooks<IInstructHook>(agent.Id);
var codeProvider = codeOptions?.CodeInterpretProvider.IfNullOrEmptyAs("botsharp-py-interpreter");
var codeProvider = codeOptions?.CodeInterpretProvider ?? "botsharp-py-interpreter";
var codeInterpreter = _services.GetServices<ICodeInterpretService>()
.FirstOrDefault(x => x.Provider.IsEqualTo(codeProvider));
@ -228,6 +228,7 @@ public partial class InstructService
// Run code script
var result = await codeInterpreter.RunCode(context.CodeScript, options: new()
{
ScriptName = scriptName,
Arguments = context.Arguments
});

View file

@ -29,6 +29,7 @@ public class PythonInterpreterPlugin : IBotSharpAppPlugin
{
var sp = app.ApplicationServices;
var settings = sp.GetRequiredService<PythonInterpreterSettings>();
var lifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
var logger = sp.GetRequiredService<ILogger<PyProgrammerFn>>();
var pyLoc = settings.InstallLocation;
@ -38,12 +39,19 @@ public class PythonInterpreterPlugin : IBotSharpAppPlugin
{
Runtime.PythonDLL = pyLoc;
PythonEngine.Initialize();
#if DEBUG
_pyState = PythonEngine.BeginAllowThreads();
#endif
var lifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
lifetime.ApplicationStopping.Register(() => {
PythonEngine.EndAllowThreads(_pyState);
PythonEngine.Shutdown();
try
{
#if DEBUG
PythonEngine.EndAllowThreads(_pyState);
#endif
PythonEngine.Shutdown();
}
catch { }
});
}
else

View file

@ -1,7 +1,6 @@
using BotSharp.Core.CodeInterpreter;
using Microsoft.Extensions.Logging;
using Python.Runtime;
using System.Threading;
using System.Threading.Tasks;
namespace BotSharp.Plugin.PythonInterpreter.Services;
@ -26,15 +25,6 @@ public class PyInterpretService : ICodeInterpretService
public async Task<CodeInterpretResult> RunCode(string codeScript, CodeInterpretOptions? options = null)
{
if (options?.LockFree != true)
{
return await _executor.Execute(async () =>
{
return InnerRunCode(codeScript, options);
}, cancellationToken: options?.CancellationToken ?? CancellationToken.None);
}
return InnerRunCode(codeScript, options);
}
@ -83,7 +73,7 @@ public class PyInterpretService : ICodeInterpretService
var list = new PyList();
if (options?.Arguments?.Any() == true)
{
list.Append(new PyString("code.py"));
list.Append(new PyString(options?.ScriptName.IfNullOrEmptyAs("script.py")));
foreach (var arg in options.Arguments)
{