Topic: How To Edit Script Run multiprocessing process ?

In Normal Script Procedure
start step1 finish
start step2 finish
start step3 finish

How To Edit Script  step1,step2,step3 start  in same time



https://i.ibb.co/9NSFrXz/2019-08-05-19-30-48.png

My Visual Database : I Love You
Easy For Beginner Student For Me

Re: How To Edit Script Run multiprocessing process ?

Hello prahousefamily,

Multi-threading is what you are looking for. Unfortunately, I do not think that MVD supports this feature (but I stand to be corrected).


To achieve what you describe, you need to be able to launch processes in separate memory spaces (threads), so that they run alongside each other without colliding, and without blocking the main thread which usually runs the graphical user interface.


Ever noticed how a long looping process can block the software ? That's why we use "application.processmessage" in the loop to keep a little control over the GUI. But when you click somewhere during a loop (drag the form for example), this halts the process until you let go.


This is what multi threading is for : running processes without freezing the UI.


Now, in your example, if you want to speed-up queries, (which can not run multi-threaded with Sqlite anyway), try to use SQL Transactions, this will speed-up things significantly.

transaction Start
SQL1
SQL2
...
SQL3
transaction commit

I can't give you the exact code because I am at the office, but you should find easily on the forum.


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: How To Edit Script Run multiprocessing process ?

Thank You mathmathou

mathmathou Thank you very much for the answer
I hope this MVD will add this Multi-threading feature.
This benefit will reduce time and can be used to process more than 1 event in a single procedure.
Additional questions if SQLite cannot do Multi-threading in MVD (must use Transaction Script instead)
And if it is MySQL, can be Multi-threading in MVD?

My Visual Database : I Love You
Easy For Beginner Student For Me

Re: How To Edit Script Run multiprocessing process ?

Hello again smile


Regarding SQLite, it is a question that it is generally single access that makes it incompatible with multi-threading.
There is an option in the sqlite "engine" where you could specify whether the LockingMode parameter is either "Exclusive" or "Normal", but it is not a very good idea to launch a second query (or transaction) when the previous access has not completed it's task.


As per MySQL (or other database engines), they can support multiple connections and operations at once, BUT, it has to be in different "sessions". A single user has to wait for a task to complete before he can trigger another one, but different users can trigger multiple tasks together, the database engine will handle the priorities on itself.


Finally, multi-threading is a complicated subject in Delphi (and other languages as well). Took me weeks to understand what it was about and months to have it working in my applications (notice that I did not say "to do it correctly" because I am far from there yet).


I do not know which connection and query components MVD uses (Firedac, Mydac...). Some are thread safe, some are not. Furthermore, a general rule of thumb is that every thread should create it's own connection for it's needs, and I am not sure you could do that in MVD : the connection is opened once you launch the application, and I am not sure you can close it if you need.


If Dmitry implements multi-threading in MVD, that would be awesome, but I suppose he has other priorities (and that is very much understandable), and such a feature would be time consuming and probably only useful for very little users.


Sorry my friend, I don't see it coming anytime soon in MVD.


Take care


Math

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: How To Edit Script Run multiprocessing process ?

Hi,

When I need to do a task in the background I launch external programs (done with MVD or another devtool) and I use a timer to get the ackknowledges  or the datas.
See the sample below.

My RSS downloader (PODCASTOR) works like that : http://codyssea.com/index.php/2017/08/20/podcastor

Regards,
jihem

var
  AgentGUID:Integer;
  AgentTimr:TTimer;
  AgentTBck:TTimer;
  AgentName:TStringList;
  AgentCBck:TStringList;

procedure AgentStart();
begin
  AgentGUID:=0;
  AgentName:=TStringList.Create();
  AgentCBck:=TStringList.Create();

  AgentTimr:=TTimer.Create(nil);
  AgentTimr.OnTimer:=@AgentOnTimer;
  AgentTimr.Interval:=2000;

  AgentTBck:=TTimer.Create(nil);
  AgentTBck.Interval:=100;
end;

procedure AgentOnTimer();
var
  Indx:Integer;
  GUID:String;
begin
  Indx:=AgentName.Count;
  If Indx>0 Then         
  Begin
    while Indx>0 do
    begin
      Dec(Indx);
      GUID:=Format('%s.txt',[AgentName[Indx]]);
      If FileExists(GUID) Then
      Begin
        AgentTBck.OnTimer:=AgentCBck[Indx];
        AgentTBck.Enabled:=True;
        AgentName.Delete(Indx);
        AgentCBck.Delete(Indx);
        DeleteFile(GUID);
        Indx:=0;
      End;
    end;
    Beep(1000,500);
  End
  Else
    AgentTimr.Enabled:=False;
end;

procedure AgentAcknowledge();
begin
  AgentTBck.Enabled:=False;
end;

procedure AgentStop();
begin
  AgentTimr.Enabled:=False;
  AgentTBck.Enabled:=False;
  AgentTimr.Free;
  AgentTBck.Free;
  AgentName.Free;
  AgentCBck.Free;
end;

procedure AgentTaskRun(Args:String;CallBack:Pointer);
var
  GUID:String;
begin
  Inc(AgentGUID);
  GUID:=IntToHex(AgentGUID,8);
  AgentName.Add(GUID);
  Args:=GUID+' '+Args;
  GUID:=Format('%s.txt',[GUID]);
  AgentCBck.Add(VarToStr(CallBack));

  if FileExists(GUID) then
  begin
    DeleteFile(GUID);
  end;
  OpenFile(Args,'agent.products\Windows\agent.exe');
  If AgentTimr.Enabled=False Then           
  begin
    AgentTimr.Enabled:=True;
  end;
end;

{ - - - }

procedure OnAF;
begin
    AgentAcknowledge;
    Form1.Button1.Caption:='Off';
    ShowMessage('Done.');
end;

procedure Form1_OnClose (Sender: TObject; Action: string);
begin
    AgentStop();
end;

procedure Form1_OnShow (Sender: TObject; Action: string);
begin
  AgentStart();
end;

procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
begin
    AgentTaskRun('XXX',@OnAF);
    Form1.Button1.Caption:='On';
end;

begin
  Form1.mniFile.Visible := False;
  Form1.mniOptions.Visible := False;
  Form1.mniSettings.Visible := False;
  Form1.mniReport.Visible := False;
  Form1.mniAbout.Visible := False;
end.
while(! success=retry());
https://jihem.itch.io