Page 1 of 1

threads don't work in parallel

Posted: 13 Jun 2021, 15:05
by braunbaer
My script needs a relatively long time to initialize all data. It has to load a 5MB file over the internet, analize this file, load a local text file, build a (big) array of object from this text file and sort this array. All this takes some time. I wanted to increase the speed by placing the main two tasks which are completely independent from each other in timed subroutines, so that they run in parallel:

Code: Select all

settimer, InitQueues, -3  
settimer, DownloadHtmlFile, -5 
...

InitQueues() {
    debug("InitQueues " xtime())
    ....
    }
    
DownloadHtmlfile() {
    t:=substr(xtime(),12)
    ...
    t1:=substr(xtime(),12)
    ...
    debug(format("DownloadHtmlfile {}-{}-{}, {} files",t,t1,substr(xtime(),12),NL.Count()))
    }

The debug output looks like this:

Code: Select all

  804  DownloadHtmlfile 19:41:10-19:41:14-19:41:32, 12885 files  <---- 19:41:32 is the ending time of this routine
  209  InitQueues 19:41:32 
So this shows that initqueues() is only started when DownloadHtmlfile is finished, no parallel processing of the two threads. It's also funny that htmldownload starts first, though it's timed to start after 5 ms while Initqueues is timed to start after 3ms.

How can I have the two threads execute in parallel?

And any other hints how to speed processing? There a lot to do, still the same job would not need more than a a few seconds in a compiled language like Delphi. I have already placed listlines, off before the main loops and placed most commands of those loops in a "single code line" using continuation lines

Re: threads don't work in parallel

Posted: 13 Jun 2021, 15:14
by boiler
Standard AutoHotkey does not support multithreading. Try AHK_H.

Re: threads don't work in parallel

Posted: 13 Jun 2021, 15:37
by braunbaer
Thank you, I'll try

Can I find somewhere a list of incompatibilities between Autohotkey and Autohotkey_h?
I read there is not much but still ;)


In the first post of the thread "About AHK_H - Downloads, Updates, Changes etc. v1 & v2", unfortunately the link "new features" is dead.
Today AutoHotkey_H is much more than AutoHotkey.dll, it has many new features, compression and protection for compiled scripts, COM Interface and it is 99% compatible with main AutoHotkey.

Re: threads don't work in parallel  Topic is solved

Posted: 13 Jun 2021, 16:58
by swagfag
It's also funny that htmldownload starts first, though it's timed to start after 5 ms while Initqueues is timed to start after 3ms.
SetTimer doesnt have that sort of granularity, no surprise there
How can I have the two threads execute in parallel?
in a single script, u cant
And any other hints how to speed processing?
even with parallelism, ur script still might be unusable. but ull have to test
or u could redesign ur script to do its loading and processing concurrently and incrementally. fetch a bit from the internet, process, load a bit, process, show a bit to the user and so on until everything's been processed
I have already placed listlines, off before the main loops and placed most commands of those loops in a "single code line" using continuation lines
use SetBatchLines -1. these "optimizations" probably account for less than 0.5%
Can I find somewhere a list of incompatibilities between Autohotkey and Autohotkey_h?
there are none. AHK_H v1 is a superset of v1. the docs are here https://hotkeyit.github.io/v2/docs/AutoHotkey.htm

Re: threads don't work in parallel

Posted: 13 Jun 2021, 17:56
by braunbaer
swagfag wrote:
13 Jun 2021, 16:58
use SetBatchLines -1. these "optimizations" probably account for less than 0.5%
Thank you, that really helped. Using SetBatchLines 200ms for the big loops reduced the total starting time of the script in a mindblowing way, from about 40 seconds to less than 10 seconds. From that point, SetBatchLines -1 does not make much difference anymore.

As far as I have understood, AHK_L would also execute the timer "threads" one after the other, and I would have to setup explicitly a threaded environment using AHKThread for having threads working in parallel? Is that correct?

Re: threads don't work in parallel

Posted: 13 Jun 2021, 19:01
by boiler
braunbaer wrote: As far as I have understood, AHK_L would also execute the timer "threads" one after the other, and I would have to setup explicitly a threaded environment using AHKThread for having threads working in parallel? Is that correct?
If that is just meant to restate what we have posted — that you need to use AHK_H, not AHK_L, to run multiple threads in parallel — then yes.

Re: threads don't work in parallel

Posted: 14 Jun 2021, 02:53
by braunbaer
Sorry, I got confused with the different AHK versions. I am talking of the threading version of AHK, which is AHK_H.

When I use AHK_H, does a timer subroutine start as new, concurrent thread? As far as I have understood, that is not the case. I would have to setup a separate thread using AhkThread. Is this correct?

Re: threads don't work in parallel

Posted: 14 Jun 2021, 04:57
by swagfag
When I use AHK_H, does a timer subroutine start as new, concurrent thread?
no
I would have to setup a separate thread using AhkThread. Is this correct?
yes, that would be one way of many different ones