threads don't work in parallel Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

threads don't work in parallel

13 Jun 2021, 15:05

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
Last edited by braunbaer on 13 Jun 2021, 15:19, edited 1 time in total.
User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: threads don't work in parallel

13 Jun 2021, 15:14

Standard AutoHotkey does not support multithreading. Try AHK_H.
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: threads don't work in parallel

13 Jun 2021, 15:37

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.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

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

13 Jun 2021, 16:58

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
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: threads don't work in parallel

13 Jun 2021, 17:56

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?
User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: threads don't work in parallel

13 Jun 2021, 19:01

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.
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: threads don't work in parallel

14 Jun 2021, 02:53

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?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: threads don't work in parallel

14 Jun 2021, 04:57

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: fiendhunter and 238 guests