eachParallel with timeout for the closure?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

eachParallel with timeout for the closure?

Peti Koch
Hi there,

I'd like to find Strings inside of a very large String using multiple regular expressions in parallel.

The program could look like this, using gpars:

        def largeText = '.... large  abc def  large ... '
       
        def regexList = [/abc/, /def/]
        // might come from configuration -> possibility for regex with catastrophic backtracking
       
        def result = [].asSynchronized()
       
        GParsExecutorsPool.withPool {
            regexList.eachParallel { String regex ->
                result.addAll(largeText.findAll(regex))
            }
        }
       
        assert result == ['abc', 'def']


Question:
How can I add a timeout for the closure { String regex -> result << largeText.findAll(regex) }
to avoid "endless" running code due catastrophic backtracking?

Best regards from Lucerne (Switzerland),
Peti

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: eachParallel with timeout for the closure?

Russel Winder-3
Hi,
On Tue, 2015-03-03 at 15:28 +0100, Peti Koch wrote:
> Hi there,
>
> I'd like to find Strings inside of a very large String using
> multiple regular expressions in parallel.

Sounds like a sensible thing to do :-)

> The program could look like this, using gpars:
>
>         def largeText = '.... large  abc def  large ... '
>
>         def regexList = [/abc/, /def/]
>         // might come from configuration -> possibility for regex
> with
> catastrophic backtracking
>
>         def result = [].asSynchronized()
>
>         GParsExecutorsPool.withPool {
>             regexList.eachParallel { String regex ->
>                 result.addAll(largeText.findAll(regex))
>             }
>         }
>
>         assert result == ['abc', 'def']
Seems the right sort of thing to do.

> Question:
> How can I add a timeout for the closure { String regex -> result <<
> largeText.findAll(regex) }
> to avoid "endless" running code due catastrophic backtracking?

The underlying Fork/Join framework provides one way of doing something
of this sort but it is not part of the way GPars harnesses it. As far
as I know, anyway. So basically the current answer has to be: No.
Sorry.

Well except that you could put the needed code into the closure
yourself. Complicated though.

I guess you just have to set up an external watchdog timer…

--
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:[hidden email]
41 Buckmaster Road    m: +44 7770 465 077   xmpp: [hidden email]
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

signature.asc (188 bytes) Download Attachment
Loading...