Forking a script execution without adding code to each thread separatedly.
Posted: 31 Oct 2018, 09:50
Hello everybody!
I have a question, which i looked a lot and couldn't find any way for it to work, and even if it is possible.
The thing is:
I have a script, where i have a lots of variables.
After i load all this variables, i want to process them in separate threads, at the same time.
For instance, i will have 4 subroutines (each one makes a REST api call, then process the retrieved data), and each one takes around 2 seconds to complete. So, the report i want to generate takes around 8 seconds to be ready. I wish to use 4 threads at least, so the time goes to 2 or 3 seconds total time, instead of 8.
After that, i display the report on a GUI.
The thing is: i DO NOT want to write all data to a file, then access this file with the threads. It looks too much time consuming. I wanted to do it in a more elegant way.
The closer i got to what i wanted was using "ahklabel" (https://hotkeyit.github.io/v2/docs/comm ... kLabel.htm), but the problem is that it creates a thread, executes it, then creates another one, execute it, and so on (which means, it does not multithread in reality, and takes the same time as if there were no threads).
Plus, i dont want to copy tons of code inside the thread code again. I wish it would just gosub to the labels from my running script, and execute it separatedly.
The example "code" i put here is very simple, and takes no time to execute.
But the my real application is done with REST API calls. As statedI call 4 rest api calls to get some json data, then do some processing afterwards. Each of this call/processing takes around 2 seconds, so my report is generated in around 8 seconds.
I have a question, which i looked a lot and couldn't find any way for it to work, and even if it is possible.
The thing is:
I have a script, where i have a lots of variables.
After i load all this variables, i want to process them in separate threads, at the same time.
For instance, i will have 4 subroutines (each one makes a REST api call, then process the retrieved data), and each one takes around 2 seconds to complete. So, the report i want to generate takes around 8 seconds to be ready. I wish to use 4 threads at least, so the time goes to 2 or 3 seconds total time, instead of 8.
After that, i display the report on a GUI.
The thing is: i DO NOT want to write all data to a file, then access this file with the threads. It looks too much time consuming. I wanted to do it in a more elegant way.
The closer i got to what i wanted was using "ahklabel" (https://hotkeyit.github.io/v2/docs/comm ... kLabel.htm), but the problem is that it creates a thread, executes it, then creates another one, execute it, and so on (which means, it does not multithread in reality, and takes the same time as if there were no threads).
Plus, i dont want to copy tons of code inside the thread code again. I wish it would just gosub to the labels from my running script, and execute it separatedly.
The example "code" i put here is very simple, and takes no time to execute.
But the my real application is done with REST API calls. As statedI call 4 rest api calls to get some json data, then do some processing afterwards. Each of this call/processing takes around 2 seconds, so my report is generated in around 8 seconds.
Code: Select all
::show report:: ;example pseudocode
workfile = october work logs ;this sets a variable before starting the new threads. some of the new threads might need this value to continue. for instance, the "readtime" label right ahead will need this value to know which .ini file to read from.
mondaytime = 2 ;number of hours worked in each week day. in here, the data is hardcoded to make the example easier. in real application, the data is read from other source, like a gui, or a REST call to a site.
tuesdaytime = 3
thursdaytime = 1
gosub, readtime ; i want to call THIS sub as another thread.
gosub, sumweektime ; i want to call THIS sub as another thread.
gosub, RESTapicallA ; i want to call THIS sub as another thread.
gosub, RESTapicallB ; i want to call THIS sub as another thread.
;here i check if the both threads are already finished, and the i display data on a gui interface.
while (threads are running) ;waits until threads finish
{
sleep 100
}
msgbox, worktime: %worktime% , sumtime: %sumtime% ;after thread finish, display results on screen.
return
;subroutines
readtime:
IniRead, worktime, %A_ScriptFullPath%, %workfile%.ini, worktime ;gets the worktime from the october logs.
return
sumweektime:
sumtime = mondaytime + tuesdaytime + thursdaytime
return
RESTapicallA:
(...) ; do rest api call, and process data
return
RESTapicallB:
(...) ; do rest api call, and process data
return