Skip to content

Commit 2079ba3

Browse files
author
Jicheng Lu
committed
remove allow thread
1 parent c30787a commit 2079ba3

File tree

5 files changed

+17
-22
lines changed

5 files changed

+17
-22
lines changed

src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace BotSharp.Abstraction.CodeInterpreter.Models;
44

55
public class CodeInterpretOptions
66
{
7+
public string? ScriptName { get; set; }
78
public IEnumerable<KeyValue>? Arguments { get; set; }
8-
public bool LockFree { get; set; }
99
public CancellationToken? CancellationToken { get; set; }
1010
}

src/Infrastructure/BotSharp.Core/CodeInterpreter/CodeScriptExecutor.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public CodeScriptExecutor(
1313
_logger = logger;
1414
}
1515

16-
public async Task<CodeInterpretResult> Execute(Func<Task<CodeInterpretResult>> func, CancellationToken cancellationToken = default)
16+
public async Task<T> Execute<T>(Func<Task<T>> func, CancellationToken cancellationToken = default)
1717
{
1818
await _semLock.WaitAsync(cancellationToken);
1919

@@ -24,11 +24,7 @@ public async Task<CodeInterpretResult> Execute(Func<Task<CodeInterpretResult>> f
2424
catch (Exception ex)
2525
{
2626
_logger.LogError(ex, $"Error in {nameof(CodeScriptExecutor)}.");
27-
return new CodeInterpretResult
28-
{
29-
Success = false,
30-
ErrorMsg = ex.Message
31-
};
27+
return default(T);
3228
}
3329
finally
3430
{

src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ await hook.OnResponseGenerated(new InstructResponseModel
155155
var db = _services.GetRequiredService<IBotSharpRepository>();
156156
var hooks = _services.GetHooks<IInstructHook>(agent.Id);
157157

158-
var codeProvider = codeOptions?.CodeInterpretProvider.IfNullOrEmptyAs("botsharp-py-interpreter");
158+
var codeProvider = codeOptions?.CodeInterpretProvider ?? "botsharp-py-interpreter";
159159
var codeInterpreter = _services.GetServices<ICodeInterpretService>()
160160
.FirstOrDefault(x => x.Provider.IsEqualTo(codeProvider));
161161

@@ -228,6 +228,7 @@ await hook.OnResponseGenerated(new InstructResponseModel
228228
// Run code script
229229
var result = await codeInterpreter.RunCode(context.CodeScript, options: new()
230230
{
231+
ScriptName = scriptName,
231232
Arguments = context.Arguments
232233
});
233234

src/Plugins/BotSharp.Plugin.PythonInterpreter/PythonInterpreterPlugin.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public void Configure(IApplicationBuilder app)
2929
{
3030
var sp = app.ApplicationServices;
3131
var settings = sp.GetRequiredService<PythonInterpreterSettings>();
32+
var lifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
3233
var logger = sp.GetRequiredService<ILogger<PyProgrammerFn>>();
3334
var pyLoc = settings.InstallLocation;
3435

@@ -38,12 +39,19 @@ public void Configure(IApplicationBuilder app)
3839
{
3940
Runtime.PythonDLL = pyLoc;
4041
PythonEngine.Initialize();
42+
#if DEBUG
4143
_pyState = PythonEngine.BeginAllowThreads();
44+
#endif
4245

43-
var lifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
4446
lifetime.ApplicationStopping.Register(() => {
45-
PythonEngine.EndAllowThreads(_pyState);
46-
PythonEngine.Shutdown();
47+
try
48+
{
49+
#if DEBUG
50+
PythonEngine.EndAllowThreads(_pyState);
51+
#endif
52+
PythonEngine.Shutdown();
53+
}
54+
catch { }
4755
});
4856
}
4957
else

src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyInterpretService.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using BotSharp.Core.CodeInterpreter;
22
using Microsoft.Extensions.Logging;
33
using Python.Runtime;
4-
using System.Threading;
54
using System.Threading.Tasks;
65

76
namespace BotSharp.Plugin.PythonInterpreter.Services;
@@ -26,15 +25,6 @@ public PyInterpretService(
2625

2726
public async Task<CodeInterpretResult> RunCode(string codeScript, CodeInterpretOptions? options = null)
2827
{
29-
if (options?.LockFree != true)
30-
{
31-
return await _executor.Execute(async () =>
32-
{
33-
return InnerRunCode(codeScript, options);
34-
}, cancellationToken: options?.CancellationToken ?? CancellationToken.None);
35-
36-
}
37-
3828
return InnerRunCode(codeScript, options);
3929
}
4030

@@ -83,7 +73,7 @@ private CodeInterpretResult CoreRun(string codeScript, CodeInterpretOptions? opt
8373
var list = new PyList();
8474
if (options?.Arguments?.Any() == true)
8575
{
86-
list.Append(new PyString("code.py"));
76+
list.Append(new PyString(options?.ScriptName.IfNullOrEmptyAs("script.py")));
8777

8878
foreach (var arg in options.Arguments)
8979
{

0 commit comments

Comments
 (0)