Page 1 of 1

commands asynchronously

Posted: Fri Aug 05, 2016 11:04 am
by link76
I would like to start the different commands asynchronously, without a command blocks/wait the next,

thx

Code: Select all

on Start

    myCommand1
    myCommand2
    myCommand3

send Start to me in 10 seconds

end Start

on myCommand1
 if browserid is empty then
         
       ....

         put lamorce into url abc
         put the windowid of stack "main"  into tID
         put revBrowserOpen(tID,abc) into browserid
          
         revBrowserSet browserid,"scrollbars","false"
         revBrowserSet browserid,"showBorder","false"
         revBrowserSet browserid,"offline","true"
         revBrowserSet browserid,"visible","true"
         revBrowserSet browserid,"rect",the rect of grc "BrowserMappa" of card "card vista_mappa" stack "main" 
      end if
end myCommand1

on myCommand2
 .....
end myCommand2

on myCommand3
 .....
end myCommand3

Re: commands asynchronously

Posted: Fri Aug 05, 2016 11:41 am
by Klaus
Buongiorno caro,

LC is single threaded, so this is not possible unfortunately!

General hints:
1. "start" is a reserverd word!
2. Always put QUOTES around the message you want to SEND:

Code: Select all

...
send "myStart" to me in 10 seconds
...
Best

Klaus

Re: commands asynchronously

Posted: Fri Aug 05, 2016 3:53 pm
by Mikey
multithreading is a bit of a kluge, but the idea is similar to what you do when you perform real multithreading.
1) Make multiple copies of LC (normally we do this with LC Server, but, if you compile your app, you can imagine how to do this)
2) The first instance launched is the main/controlling app/thread
3) It then launches the others
4) Each one communicates either via a net interface (but you use 127.0.0.1 for the address, so the traffic never leaves the machine), a file, sending messages to the other app/thread, or a database.

This is not ideal, but it does work.

Re: commands asynchronously

Posted: Fri Aug 05, 2016 7:33 pm
by FourthWorld
Like Python, LC is single-threaded and as such does not inherently support concurrency. Python does support logical parallelism via its “GIL” (Global Interpreter Lock) in a way that is arguably cleaner than doing quasi-parallelism in LC, there are still some ways background processing can be done within LC's single-threaded process - this stack illustrates some of that:
http://fourthworld.net/channels/lc/IdleHour.rev

(For a great discussion on the difference between parallelism and concurrency see Rob Pike's talk: https://www.youtube.com/watch?v=cN_DpYBzKso )

Beyond simulated multithreading is multiprocessing, as Mikey suggested. While multiprocessing isn't quite as efficient on Windows systems as it is on macOS or Linux, it can still be a good fit for many tasks where you need true concurrency across cores to drive heavy workloads.

If that sounds like your need there are many ways to coordinate multiple processes, perhaps most commonly with localhost sockets, which LiveCode can handle very well.