freelanceprogrammers.org Forum Index » Delphi

No juice for threads started with thread.start_new in Python


View user's profile Post To page top
kxroberto Posted: Thu Dec 21, 2006 7:56 pm


Joined: 21 Dec 2006

Posts: 8
No juice for threads started with thread.start_new in Python
When I start a python thread from within a VarPyth call into Python ..

//Delphi
dcore.StartJob(dcore.somethread)


.. the thread.start_new started thread there doesn`t do something
unless I feed Python (GIL?) from the main delphi thread with
dcore.time.sleep(0.1) or frequent Python execution or so - which means
no real parallel execution of Delphi and Python thread.
Seems the GIL/Thread lock is not released by default.

How to cure this?

Is there just some PyEval_InitThreads( ) / PyEval_SaveThread() /
PyEval_RestoreThread() processing missing in TPythonEngine & VarPyth
processing?

( P4D v3.25, Py2.3 )


Robert
Reply with quote
Send private message
View user's profile Post To page top
samuel_iseli Posted: Fri Dec 22, 2006 8:06 pm


Joined: 22 May 2006

Posts: 6
No juice for threads started with thread.start_new in Python
PythonEngine acquires the GIL when initialized.
to allow other threads to run you need to release it, when you are
finished with calling python from delphi.

i found the easiest way is to use the PyEval_SaveThread /
PyEval_RestoreThread API methods and saving a threadstate for your
main thread.

codewise, that`s:

var
myThreadState: PPyThreadState;

// call python from your delphi main thread
//...
myThreadState:=GetPythonEngine.PyEval_SaveThread;
// other threads are allowed to run
//....
GetPythonEngine.PyEval_RestoreThread(myThreadState);
// calls to python from your main delphi thread allowed again
//....

hope this helps
Samuel

--- In pythonfordelphi@yahoogroups.com, "kxroberto" <kxroberto@...> wrote:
>
> When I start a python thread from within a VarPyth call into Python ..
>
> //Delphi
> dcore.StartJob(dcore.somethread)
>
>
> .. the thread.start_new started thread there doesn`t do something
> unless I feed Python (GIL?) from the main delphi thread with
> dcore.time.sleep(0.1) or frequent Python execution or so - which means
> no real parallel execution of Delphi and Python thread.
> Seems the GIL/Thread lock is not released by default.
>
> How to cure this?
>
> Is there just some PyEval_InitThreads( ) / PyEval_SaveThread() /
> PyEval_RestoreThread() processing missing in TPythonEngine & VarPyth
> processing?
>
> ( P4D v3.25, Py2.3 )
>
>
> Robert
>
Reply with quote
Send private message
View user's profile Post To page top
kxroberto Posted: Tue Dec 26, 2006 6:08 pm


Joined: 21 Dec 2006

Posts: 8
No juice for threads started with thread.start_new in Python
Thanks. Found that when one uses the VarPyth Interface intensively
throughout the app (and doing the PyEval_SaveThread.. everytime is
tedious) its more easy to do it through an Application.OnIdle like:

procedure MyIdleHandler(Sender: TObject; var Done: Boolean);
begin
dcore.retrieve_interthreadcalls();
dcore.time.sleep(0.005);
Done:=False; //forces continuous processing
end;

.. which feeds Python threading through time.sleep as well - whenever
the GUI doesn`t do something else.
(And possibly feed Python also through a timer or explicitely during
modal message loops and do PyEval_SaveThread.. only when there are
expensive computations.


Robert


--- In pythonfordelphi@yahoogroups.com, "samuel_iseli"
<samuel.iseli@...> wrote:
>
> PythonEngine acquires the GIL when initialized.
> to allow other threads to run you need to release it, when you are
> finished with calling python from delphi.
>
> i found the easiest way is to use the PyEval_SaveThread /
> PyEval_RestoreThread API methods and saving a threadstate for your
> main thread.
>
> codewise, that`s:
>
> var
> myThreadState: PPyThreadState;
>
> // call python from your delphi main thread
> //...
> myThreadState:=GetPythonEngine.PyEval_SaveThread;
> // other threads are allowed to run
> //....
> GetPythonEngine.PyEval_RestoreThread(myThreadState);
> // calls to python from your main delphi thread allowed again
> //....
>
> hope this helps
> Samuel
>
> --- In pythonfordelphi@yahoogroups.com, "kxroberto" <kxroberto@> wrote:
> >
> > When I start a python thread from within a VarPyth call into Python ..
> >
> > //Delphi
> > dcore.StartJob(dcore.somethread)
> >
> >
> > .. the thread.start_new started thread there doesn`t do something
> > unless I feed Python (GIL?) from the main delphi thread with
> > dcore.time.sleep(0.1) or frequent Python execution or so - which means
> > no real parallel execution of Delphi and Python thread.
> > Seems the GIL/Thread lock is not released by default.
> >
> > How to cure this?
> >
> > Is there just some PyEval_InitThreads( ) / PyEval_SaveThread() /
> > PyEval_RestoreThread() processing missing in TPythonEngine & VarPyth
> > processing?
> >
> > ( P4D v3.25, Py2.3 )
> >
> >
> > Robert
> >
>
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
English Courses in England - reservation calendar script
Land Surveying -Stonex instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help