[英]Reset Python4Delphi engine?
I use D7 with Python4Delphi. 我将D7与Python4Delphi一起使用。 After users have imported much of py-files, Python have all these modules cached. 用户导入了很多py文件后,Python会将所有这些模块缓存起来。 I need a way to reset Py engine. 我需要一种重置Py引擎的方法。 So that Py "forgets" all user-imported modules, and I have "clean" Python, w/out restarting the app. 这样Py会“忘记”所有用户导入的模块,而我将拥有“干净的” Python,而无需重启应用程序。 How to do it? 怎么做?
It should be sufficient to destroy and re-create the TPythonEngine
object: 销毁并重新创建TPythonEngine
对象应该足够了:
OriginalOwner := GetPythonEngine.Owner;
GetPythonEngine.Free;
TPythonEngine.Create(OriginalOwner);
Destroying it calls Py_Finalize
, which frees all memory allocated by the Python DLL. 销毁它会调用Py_Finalize
,它将释放Python DLL分配的所有内存。
Or, if you're just using the Python API without the VCL wrappers, you can probably just call Py_NewInterpreter
on your TPythonInterface
object to get a fresh execution environment without necessarily discarding everything done before. 或者,如果您仅使用不带VCL包装器的Python API,则可能只需在TPythonInterface
对象上调用Py_NewInterpreter
即可获得全新的执行环境,而不必丢弃之前所做的一切。
There is a demo showing you how to unload/reload python using P4D at https://github.com/pyscripter/python4delphi/tree/master/PythonForDelphi/Demos/Demo34 . 在https://github.com/pyscripter/python4delphi/tree/master/PythonForDelphi/Demos/Demo34上有一个演示向您展示如何使用P4D卸载/重新加载python。 The key method that (re)creates the python components and (re)loads different versions of python is shown below: 下面显示了(重新)创建python组件并(重新)加载不同版本的python的关键方法:
procedure TForm1.CreatePythonComponents;
begin
if cbPyVersions.ItemIndex <0 then begin
ShowMessage('No Python version is selected');
Exit;
end;
// Destroy P4D components
FreeAndNil(PythonEngine1);
FreeAndNil(PythonType1);
FreeAndNil(PythonModule1);
{ TPythonEngine }
PythonEngine1 := TPythonEngine.Create(Self);
PyVersions[cbPyVersions.ItemIndex].AssignTo(PythonEngine1);
PythonEngine1.IO := PythonGUIInputOutput1;
{ TPythonModule }
PythonModule1 := TPythonModule.Create(Self);
PythonModule1.Name := 'PythonModule1';
PythonModule1.Engine := PythonEngine1;
PythonModule1.ModuleName := 'spam';
with PythonModule1.Errors.Add do begin
Name := 'PointError';
ErrorType := etClass;
end;
with PythonModule1.Errors.Add do begin
Name := 'EBadPoint';
ErrorType := etClass;
ParentClass.Name := 'PointError';
end;
{ TPythonType }
PythonType1 := TPythonType.Create(Self);
PythonType1.Name := 'PythonType1';
PythonType1.Engine := PythonEngine1;
PythonType1.OnInitialization := PythonType1Initialization;
PythonType1.TypeName := 'Point';
PythonType1.Prefix := 'Create';
PythonType1.Services.Basic := [bsRepr,bsStr,bsGetAttrO,bsSetAttrO];
PythonType1.TypeFlags :=
[tpfHaveGetCharBuffer,tpfHaveSequenceIn,tpfHaveInplaceOps,
tpfHaveRichCompare,tpfHaveWeakRefs,tpfHaveIter,tpfHaveClass,tpfBaseType];
PythonType1.Module := PythonModule1;
PythonEngine1.LoadDll;
end;
The demo uses the unit PythonVersions to discover installed python versions. 该演示使用PythonVersions单元发现已安装的python版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.