commands asynchronously

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
link76
Posts: 99
Joined: Fri Nov 04, 2011 1:52 pm

commands asynchronously

Post by link76 » Fri Aug 05, 2016 11:04 am

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

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: commands asynchronously

Post by Klaus » Fri Aug 05, 2016 11:41 am

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

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: commands asynchronously

Post by Mikey » Fri Aug 05, 2016 3:53 pm

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.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: commands asynchronously

Post by FourthWorld » Fri Aug 05, 2016 7:33 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply