Recent

Author Topic: Is there any similar to async/await ?  (Read 7007 times)

k1attila1

  • Full Member
  • ***
  • Posts: 105
Is there any similar to async/await ?
« on: December 14, 2018, 08:18:18 am »

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Is there any similar to async/await ?
« Reply #1 on: December 14, 2018, 09:45:30 am »
Hi


Or  similar to  Delphi  system.threading :

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Tutorial:_Using_Tasks_from_the_Parallel_Programming_Library

thank you
there are a number of 3rd party libraries you could evaluate
1) pasmp https://github.com/BeRo1985/pasmp
2) asyncCalls https://github.com/ahausladen/AsyncCalls

some might need a bit of attention to make them work with fpc but it is possible. You could also download the community edition of delphi and port the parallel library to fpc.

MountainQ

  • Jr. Member
  • **
  • Posts: 65
Re: Is there any similar to async/await ?
« Reply #2 on: December 14, 2018, 11:19:09 am »
A word of caution in case you found  the keywords 'async/await' in python:
this type of concurrency is *very* different; it is not thread-based, rather the execution of a routine has to be 'manually interrupted'.
Best

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Is there any similar to async/await ?
« Reply #3 on: December 14, 2018, 02:56:27 pm »
A word of caution in case you found  the keywords 'async/await' in python:
this type of concurrency is *very* different; it is not thread-based, rather the execution of a routine has to be 'manually interrupted'.
Best
There are 2 multi threading models. 1) preemptive multitasking where the task manager will loop through the active threads stopping and running them as needed. 2) cooperative multitasking where each thread must yeld control to the manager to run the next thread in regular intervals.
Both models use threads with the same properties/abilities, only the manager/scheduler is different.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Is there any similar to async/await ?
« Reply #4 on: December 14, 2018, 03:19:33 pm »
There are 2 multi threading models. 1) preemptive multitasking where the task manager will loop through the active threads stopping and running them as needed. 2) cooperative multitasking where each thread must yeld control to the manager to run the next thread in regular intervals.
Both models use threads with the same properties/abilities, only the manager/scheduler is different.

Cooperative multitasking is more doable in VM languages, where each entry into the VM may cause the thread to yield if necessary.

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Is there any similar to async/await ?
« Reply #5 on: December 14, 2018, 06:06:03 pm »
There are 2 multi threading models. 1) preemptive multitasking where the task manager will loop through the active threads stopping and running them as needed. 2) cooperative multitasking where each thread must yeld control to the manager to run the next thread in regular intervals.
Both models use threads with the same properties/abilities, only the manager/scheduler is different.

Cooperative multitasking is more doable in VM languages, where each entry into the VM may cause the thread to yield if necessary.
I have no experience with VM languages and that sounds something I should know already. Any references to read upon it? I'd like to understand your comment.

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Is there any similar to async/await ?
« Reply #6 on: December 14, 2018, 07:45:36 pm »
I have no experience with VM languages and that sounds something I should know already. Any references to read upon it? I'd like to understand your comment.
A VM language is essentially an interpreted language.  The interpreter is part of the VM.  Because it's interpreted, every statement goes to the interpreter/virtual CPU which gives the CPU control over the VM (at every interpreted statement), in turn, the CPU could decide to do something other than execute/interpret the statement (such as switching to another thread and execute its statements.)

In a real machine, the scheduler gets to execute when a hardware interrupt, which has been programmed for that specific purpose, transfers control to it.  The scheduler has to wait for that hardware interrupt to occur to gain control, in a VM it does not, since every interpreted instruction essentially transfers control to it.  (i.e, every interpreted statement is an "interrupt" that transfers control to the CPU (virtual CPU/interpreter, that is.)

NOTE: the above is the simplest and easiest to understand case.  In a multi-core environment, there is more than one core that can be interrupted and directed to execute the OS scheduler (the real, running on the hardware, scheduler). Obviously, the multi-core environment requires more sophisticated algorithms.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Is there any similar to async/await ?
« Reply #7 on: December 14, 2018, 08:16:14 pm »
I have no experience with VM languages and that sounds something I should know already. Any references to read upon it? I'd like to understand your comment.
A VM language is essentially an interpreted language.  The interpreter is part of the VM.  Because it's interpreted, every statement goes to the interpreter/virtual CPU which gives the CPU control over the VM (at every interpreted statement), in turn, the CPU could decide to do something other than execute/interpret the statement (such as switching to another thread and execute its statements.)
I see. I assume that marcov's comment means that because of the nature of things you mentioned, it is easier to handle "uncooperative" threads in a cooperative scheduler and that's why it is mostly seen in vm languages.
It mostly makes sense.

thank you.

mr-highball

  • Full Member
  • ***
  • Posts: 233
    • Highball Github
Re: Is there any similar to async/await ?
« Reply #8 on: December 14, 2018, 08:33:11 pm »
ezthreads has an await method that's similar to the c# await operator. This question was asked a day or two ago, and I provided a few examples,
https://forum.lazarus.freepascal.org/index.php/topic,43521.0.html

repo: https://github.com/mr-highball/ezthreads

here are some examples from the tester app:
https://github.com/mr-highball/ezthreads/blob/master/test/ezthreads_tester.lpr#L181
https://github.com/mr-highball/ezthreads/blob/master/test/ezthreads_tester.lpr#L208
« Last Edit: December 14, 2018, 08:37:40 pm by mr-highball »

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Is there any similar to async/await ?
« Reply #9 on: December 14, 2018, 08:52:04 pm »
I see. I assume that marcov's comment means that because of the nature of things you mentioned, it is easier to handle "uncooperative" threads in a cooperative scheduler and that's why it is mostly seen in vm languages.
It mostly makes sense.

thank you.
Yes, because in a VM there cannot be an "uncooperative" thread (at least not in a reasonably well designed VM.)  If the thread wants to execute, the very fact of executing a statement returns control to the virtual CPU which may decide to do something other than executing the statement. 

In a real machine, executing an instruction does not enable the CPU to decide to run scheduler code.    It depends on a hardware interrupt for that (or a call to some function(s) which lead to scheduler code, which is what Windows pre-XP depends on.)

You're welcome.  HTH.

« Last Edit: December 14, 2018, 08:53:39 pm by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Is there any similar to async/await ?
« Reply #10 on: December 14, 2018, 10:15:18 pm »
mr-highball: similar named methods don't mean they are cooperative multitasking, like some other languages have.

On Windows there are the various waitformultiple variants, which are even more powerful.

In such case, all threads have an tevent you can wait on, like await, but you can select the threads you want to wait on (in case there are many).

Moreover, you can give a timeout and check for that and run the message pump in the meanwhile.

E.g. in one of my apps (Delphi, but Lazarus is totally similar) I

  • fire up a couple of threads that do the initialization, and hold a list of their tevents.
  • Then I show splashscreen.
  • I then enter a loop like this:

Code: [Select]
  while not waitformultipleobjects(list,450) and (iterations>0) do  // 500ms
    begin       
      application.processmessages; // process events
      checkthreadlistfornewmsgsandshowonsscreen();
      dosedsleep(50); // runs application.processmessages in a loop with a sleep till 50 ms are done to process
      dec(iterations);
    end;
 

This shows the splashscreen for  iterations*(450+50) ms while being somewhat responsive (resizing, hide/display) and showing messages from the threads that they log to a threadlist.

Afaik this is old, and there are also variants that wait on (Tevent-) handles AND windows messages. Implementing this would make the response to windows messages a bit less sluggish, but I haven't bothered because the hardware that had the real long startup times is EOL.

mr-highball

  • Full Member
  • ***
  • Posts: 233
    • Highball Github
Re: Is there any similar to async/await ?
« Reply #11 on: December 15, 2018, 02:22:34 am »
@marcov agreed, that just because something is named the same functionality may differ, but the original question was whether there was something similar to await or the delphi task library. Maybe I am biased because I'm the author, but in my opinion ezthreads do offer that similarity as well as an easy way to accomplish cooperative multitasking through the use of the include await method. ezthreads all have a grouping identifier and new "tasks" can be grouped with others. There are overloads for await covering these situations:

1.) Await a single thread operation
2.) Await all threads in a group
3.) Await all threads at time of calling await

All ezthreads once started operate in their own context, so as long as the hardware is present and supportive of multithreading cooperative tasks can be done in tandem.

Additionally, ezthreads supports nested methods and "capturing" of local variables to be used by the thread, and are reference counted, providing a close enough experience in my opinion to anonymous methods supported by the delphi task library.

k1attila1

  • Full Member
  • ***
  • Posts: 105
Re: Is there any similar to async/await ?
« Reply #12 on: December 16, 2018, 08:45:48 am »
thanks to all

especially for mr-highball for ezthreads

thanks again

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Is there any similar to async/await ?
« Reply #13 on: December 16, 2018, 10:05:23 am »
There's one more solution that is not mentioned and that is to use the Observer pattern as implemented on e.g. Tpersistent.
As long as your main object implements IFPObserver you can implement asynchronous actions if any object that implements IFPObserved changes its state.
This feature is often overlooked.
A simpllified example I adapted from code on the mailing list (original by Graeme Geldenhuys)  is like this:
[edit] I changed the example a bit, again. A Tlist descendant shows better what is possible.
Code: Pascal  [Select][+][-]
  1. {$ifdef mswindows}{$apptype console}{$endif}{$mode objfpc}
  2. uses
  3.  Classes,contnrs;
  4. type
  5.   TMyObserver = class(TObject, IFPObserver)
  6.   private
  7.     procedure FPOObservedChanged(ASender : Tobject; Operation : TFPObservedOperation; Data : Pointer);
  8.   end;
  9.  
  10. procedure TMyObserver.FPOObservedChanged(ASender: TObject;
  11.                Operation: TFPObservedOperation; Data: Pointer);
  12. begin
  13.   case operation of
  14.     ooChange,        // The observed object has changed.
  15.     ooAddItem,       // An item is added to the observed object (generally a list).
  16.     ooDeleteItem,    // An item is deleted from the observed object (generally a list).
  17.     ooCustom:{empty};// Your custom event
  18.     ooFree:          // The observed object is being freed.
  19.       (ASender as IFPObserved).FPODetachObserver(self);
  20.   end;
  21.   writeln(ASender.ClassName,' has changed [',Operation,']');
  22. end;
  23.  
  24. var
  25.   Ol: TObjectlist;
  26.   Observer: TMyObserver;
  27. begin
  28.   Ol := TObjectlist.Create;
  29.   Ol.OwnsObjects := true;
  30.   Observer := TMyObserver.Create;
  31.   (Ol as IFPObserved).FPOAttachObserver(Observer);
  32.   Ol.Add(TObject.Create);  // async observer calls
  33.   Ol.Add(TObject.Create);
  34.   Ol.Delete(0);      
  35.   Ol.Free;
  36.   Observer.Free;
  37. end.

If you don't need one of the classes below you must implement IFPObserved yourself.
Classes that use it by default are e.g. Tlist, TStrings and TCollection/TCollectionItem. TPersistent partially implements it.

This is close to the VM model and cooperative multitasking.
« Last Edit: December 16, 2018, 03:04:33 pm by Thaddy »
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Is there any similar to async/await ?
« Reply #14 on: December 16, 2018, 01:19:01 pm »
@marcov agreed, that just because something is named the same functionality may differ, but the original question was whether there was something similar to await or the delphi task library. Maybe I am biased because I'm the author, but in my opinion ezthreads do offer that similarity as well as an easy way to accomplish cooperative multitasking through the use of the include await method.

Clear, and you are right. I mostly reacted to further disambiguate MountainQ's  comments, but the OP never mentioned Python indeed.


 

TinyPortal © 2005-2018