Message11495

Author jeff.allen
Recipients Ivan, jeff.allen
Date 2017-07-23.16:34:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1500827683.35.0.416251005761.issue2609@psf.upfronthosting.co.za>
In-reply-to
Content
I was unable to reproduce this on my Windows box. Here's what I evolved Ivan's demonstration into:

import thread
import re
import time
import random

LEN = 2000
COUNT = 10000
REGEX = re.compile('((?:foo|bar)+)(\d*)E?')

counter = 0
counter_lock = thread.allocate_lock()

def count(inc=0):
    global counter
    with counter_lock:
        counter += inc
        return counter
    
def parse(line):
    try:
        m = REGEX.search(line)
    finally:
        count(1)
        if LEN <= 10 and m.groups():
            print line, m.group(1)

def make():
    nonsense = list()
    for i in range(LEN):
        nonsense.append(random.choice(('foo', 'bar')))
    nonsense.append(str(random.randint(1, 99999)))
    nonsense.append(random.choice(('', 'E')))
    return ''.join(nonsense)


# Make up material
tests = list()
for i in range(COUNT):
    tests.append(make())

print "Start all threads"
start = thread.start_new_thread
for test in tests:
    start(parse, (test,))

print "Wait for completion"
while True:
    c = count()
    print c
    if not c < COUNT:
        break
    time.sleep(0.1)

I know there's a lock here, but it doesn't interfere until after the threads complete.

I get:
 ..\inst\bin\jython .\iss2609_parse2.py
Start all threads
Wait for completion
9984
10000
and no exceptions. During the run, jvisualvm peaks at 1038 live threads and the resource monitor shows all 4 CPUs busy, so I think I'm getting as much concurrency out of it as I have available. I am using:
Jython 2.7.1 (default:0df7adb1b397, Jun 30 2017, 19:02:43)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_141
History
Date User Action Args
2017-07-23 16:34:43jeff.allensetmessageid: <1500827683.35.0.416251005761.issue2609@psf.upfronthosting.co.za>
2017-07-23 16:34:43jeff.allensetrecipients: + jeff.allen, Ivan
2017-07-23 16:34:43jeff.allenlinkissue2609 messages
2017-07-23 16:34:42jeff.allencreate