Git Product home page Git Product logo

Comments (8)

pyscripter avatar pyscripter commented on June 19, 2024

Please see https://github.com/pyscripter/python4delphi/wiki/PythonThreads

from python4delphi.

KhurshedUlugov avatar KhurshedUlugov commented on June 19, 2024

Unfortunately, nothing works. None of the methods given in the article. There is simply no response. I have logging, but nothing is written in the log.

What am I doing wrong? Python embedded version is 3.8. Delphi 11.3

Here is my code:

    ThreadPythonExec(
    procedure
    begin
      GetPythonEngine.ExecFile(PyFileName);
    end{, nil, false, emNewInterpreter});

    Task := TTask.Create(
        procedure
        var
          Py: IPyEngineAndGIL;
        begin
          Py := SafePyEngine;
          Py.PythonEngine.ExecFile(PyFileName);
        end);
    Task.Start;

from python4delphi.

pyscripter avatar pyscripter commented on June 19, 2024

Are you doing this?

You should call Py_Begin_Allow_Threads in your main thread, after the python DLL was loaded, for example in your FormCreate event handler.

Also have a look at Demo 33.

from python4delphi.

KhurshedUlugov avatar KhurshedUlugov commented on June 19, 2024

Thanks for the help, it really worked. But the problem hasn't completely disappeared.
I call TPythonThread.Py_Begin_Allow_Threads when I create a form. I also call TPythonThread.Py_End_Allow_Threads when closing the form. Everything is fine, everything works fine. But if I close the form and start the form again, then there is a problem in the script with the numpy module again(other packages seem to be fine). I get an error:

C:\A\34\s/Objects/structsseq.c:398: bad argument to internal function

When I close the form, I unload the dll:

procedure TfrmScripts.FormDestroy(Sender: TObject);
begin
PyEngine.UnloadDll;
TPythonThread.Py_End_Allow_Threads; // Acquire the GIL
FreeAndNil(PyEngine);
inherited;
end;

What am I doing wrong?

from python4delphi.

pyscripter avatar pyscripter commented on June 19, 2024

Why do you free the engine?

Destroying and recreating the engine is not a good idea.

from python4delphi.

KhurshedUlugov avatar KhurshedUlugov commented on June 19, 2024

I create PythonEngine dynamically on form create. But it doesn't make any difference. Even if I use TPythonEngine component (put ting it on a form, so I don't need destroy it on closing form), still this error occurs. I have an MDI application. What do you recommend?

Put TPythonEngine component in Data Module?

Destroying and recreating the engine is not a good idea.
Why?

from python4delphi.

pyscripter avatar pyscripter commented on June 19, 2024

It makes no difference if you put the PythonEngine on a Form or a DataModule. Are you releasing the GIL before running python code again?

I am not sure what causes the error in your program, but this is not a P4D issue. You may get some help if you use the Support forum.

from python4delphi.

KhurshedUlugov avatar KhurshedUlugov commented on June 19, 2024

Are you releasing the GIL before running python code again?

on form create:
TPythonThread.Py_Begin_Allow_Threads;

on form destroy:
TPythonThread.Py_End_Allow_Threads;

from python4delphi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.