Recent

Author Topic: Python4Lazarus and multithreaded applications  (Read 3100 times)

kapibara

  • Hero Member
  • *****
  • Posts: 610
Python4Lazarus and multithreaded applications
« on: August 11, 2019, 02:56:24 am »
Is it possible to run more than one PythonEngine from threads inside an app?

When I try that, it says "There is already one instance of TPythonEngine running". Maybe there is a workaround?
« Last Edit: August 11, 2019, 07:00:24 pm by kapibara »
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Python4Lazarus and multithreaded applications
« Reply #1 on: October 13, 2020, 01:09:39 am »
hello,
Up  ;D   
Is it possible ?   
Demo11 ?  (not converted  from Delphi to Lazarus)
Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

AlexTP

  • Hero Member
  • *****
  • Posts: 2383
    • UVviewsoft
Re: Python4Lazarus and multithreaded applications
« Reply #2 on: October 13, 2020, 01:38:47 am »

Blade

  • Full Member
  • ***
  • Posts: 177
Re: Python4Lazarus and multithreaded applications
« Reply #3 on: October 13, 2020, 10:45:11 pm »
I look at Python4Delphi and just shake my head.  Embarcadero likes going on those suicide missions.  Proof positive what happens when the marketing team or some executives have lost their minds.

As for Lazarus, rather see more pas2js (https://wiki.freepascal.org/pas2js)




Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Python4Lazarus and multithreaded applications
« Reply #4 on: October 14, 2020, 10:48:41 am »
hello,
Finally, i have succeeded using TPythonThread of Python4Lazarus package.
1 - There is an error in the file PythonEngine.pas of the Python4Lazarus package :
it lacks the write for the ThreadExecMode property of the TPythonThread class
near the line 3127 replace
Code: Pascal  [Select][+][-]
  1.     property ThreadExecMode: TThreadExecMode read fThreadExecMode;  
with                              
Code: Pascal  [Select][+][-]
  1.     property ThreadExecMode: TThreadExecMode read fThreadExecMode
  2.                                                  write fThreadExecMode;

2 - I have created a small project (in attachment) which executes this python script in 3 threads in the same time :
Code: Pascal  [Select][+][-]
  1. # coding: utf8
  2. # script for testing PythonThread
  3. # Jurassic Pork october 2020
  4. import time
  5. print('Coundown Thread1 :')
  6. time.sleep(1.0)
  7. i = 1
  8. while i < 5:
  9.      print('Thread1 : ',5 - i)
  10.      i += 1
  11.      time.sleep(1.0)
  12. print('Thread1 terminated')

The print are send in the console.
project has been tested with Lazarus 2.0.10 on Windows 10 and with Lazarus 2.0.8 on Centos 8.1
In attachment my configuration of the PythonEngine component on Windows.
on Centos 8 :
Quote
DllName : libpython3.6m.so
DllPath :  /usr/lib64
RegVersion : 3.6

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Python4Lazarus and multithreaded applications
« Reply #5 on: October 14, 2020, 11:53:48 am »
I look at Python4Delphi and just shake my head.  Embarcadero likes going on those suicide missions.  Proof positive what happens when the marketing team or some executives have lost their minds.

What do you mean? Python4Delphi was started at a time when Delphi still belonged to Borland and has nothing to do with Embarcadero. Also are you maybe confusing this with Delphi for PHP? That was something completely different.

As for Lazarus, rather see more pas2js (https://wiki.freepascal.org/pas2js)

Python4Delphi is about running/using Python scripts from Delphi/FPC code. pas2js is about transpiling Object Pascal code to JavaScript. To completely different concepts and use cases. If you want to highlight something JavaScript related you should mention BESEN which is a JavaScript engine that's written in Object Pascal and which should be able to execute code transpiled by pas2js.

AlexTP

  • Hero Member
  • *****
  • Posts: 2383
    • UVviewsoft
Re: Python4Lazarus and multithreaded applications
« Reply #6 on: October 14, 2020, 12:30:56 pm »
@Jurassic Pork
I posted your fix for "write" and your demo to Py4Lazarus repo.
Next time, pls inform me. I don't read the forum always

Blade

  • Full Member
  • ***
  • Posts: 177
Re: Python4Lazarus and multithreaded applications
« Reply #7 on: October 29, 2020, 09:35:24 pm »
What do you mean? Python4Delphi was started at a time when Delphi still belonged to Borland and has nothing to do with Embarcadero. Also are you maybe confusing this with Delphi for PHP? That was something completely different.

I see Python4Delphi as vastly more advantageous to Python users (and then Lazarus users), than it is for Delphi (which is supposedly a profit making product for Embarcadero).  True, it is something that Embarcadero didn't create, but they are promoting it on YouTube and their website.  It's one thing if they were just ignoring or not openly acknowledging it, but active promotion is something else.  Especially if Embarcadero is going to turn around and hope users will fork over serious cash, maintain subscriptions, and/or get Delphi certifications.  Isn't Python popular enough?  Why would Embarcadero help make it more popular, and shoot itself in the foot?  Just seems like a different strategy would be taken, if one is playing to win or become more dominant.

Attempting to access some popular Python libraries is all well and good for casuals and hobbyist, but for Embarcadero they should arguably be attempting to push the creation of competing libraries in the Object Pascal language.  Instead of running over to Python to "steal" some goodies, make or promote the creation of such libraries in Object Pascal, if they are really useful or needed.  Would make more business sense, if they are going to play Viking and not promote Object Pascal libraries, then just make it easier to import Python/C/C++ libraries into Delphi projects as a one way exchange.  As equally insane for Embarcadero, businesswise, is allowing the promotion of Python as a scripting language for Delphi users or in their applications as oppose to DWScript/Pascal Script or variant.  Gives the impression of trying to jump on a bandwagon, but not having clearly thought out that hitchhiking on that ride might not be for the best.  It appears that Python would be way more of a threat to their bottom line than supporting or collaborating with Free Pascal/Lazarus.

For Python coders, seems like they can squeeze some free milk from the Embarcadero Delphi cow.  There is of course Free Pascal/Lazarus for that, but as another open source project, that makes way more sense as an exchange.  Python4Lazarus, not arguing against such an arrangement.  Python4Delphi?  Kind of questioning the business sense of promoting such versus being quiet about it.
« Last Edit: October 30, 2020, 02:39:18 am by Blade »

alaa123456789

  • Sr. Member
  • ****
  • Posts: 260
  • Try your Best to learn & help others
    • youtube:

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Python4Lazarus and multithreaded applications
« Reply #9 on: October 30, 2020, 09:30:44 am »
For Python coders, seems like they can squeeze some free milk from the Embarcadero Delphi cow.  There is of course Free Pascal/Lazarus for that, but as another open source project, that makes way more sense as an exchange.  Python4Lazarus, not arguing against such an arrangement.  Python4Delphi?  Kind of questioning the business sense of promoting such versus being quiet about it.

But it's not the fault of the Python4Delphi project that Embarcadero is doing this and your statement I had quoted earlier felt as if you blame that project's existence for that.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Python4Lazarus and multithreaded applications
« Reply #10 on: October 30, 2020, 11:26:28 am »
Jurassic Pork: you use Python 3.6, but wasn't Python4Delphi meant for 2.x ?  On Windows it came with a 2.6 dll.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Python4Lazarus and multithreaded applications
« Reply #11 on: October 30, 2020, 01:55:05 pm »
hello,
Jurassic Pork: you use Python 3.6, but wasn't Python4Delphi meant for 2.x ?  On Windows it came with a 2.6 dll.
in the file Definition.inc of the project python4Lazarus we can find :
Quote
//{$DEFINE PYTHON25}
//{$DEFINE PYTHON26}
//{$DEFINE PYTHON27}
//{$DEFINE PYTHON32}
//{$DEFINE PYTHON33}
//{$DEFINE PYTHON34}
//{$DEFINE PYTHON35}
//{$DEFINE PYTHON36}
//{$DEFINE PYTHON37}
//{$DEFINE PYTHON38}

i think that they are the python versions  which are compatible with python4lazarus.

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

 

TinyPortal © 2005-2018