File: latch.py

package info (click to toggle)
python-mitogen 0.3.26-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,456 kB
  • sloc: python: 22,134; sh: 183; makefile: 74; perl: 19; ansic: 18
file content (51 lines) | stat: -rw-r--r-- 1,130 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Used for stressing Latch.get/put. Swap the number of producer/consumer threads
below to try both -- there are many conditions in the Latch code that require
testing of both.
"""

import logging
import random
import threading
import time
import mitogen.core
import mitogen.utils

mitogen.utils.log_to_file()
mitogen.core.IOLOG.setLevel(logging.DEBUG)
mitogen.core._v = True
mitogen.core._vv = True

l = mitogen.core.Latch()
consumed = 0
produced = 0
crash = 0

def cons():
    global consumed, crash
    try:
        while 1:
            g = l.get()
            print('got=%s consumed=%s produced=%s crash=%s' % (g, consumed, produced, crash))
            consumed += 1
            time.sleep(g)
            for x in range(int(g * 1000)):
                pass
    except:
        crash += 1

def prod():
    global produced
    while 1:
        l.put(random.random()/10)
        produced += 1
        time.sleep(random.random()/10)

allc = [threading.Thread(target=cons) for x in range(64)]
allp = [threading.Thread(target=prod) for x in range(8)]
for th in allc+allp:
    th.setDaemon(True)
    th.start()

raw_input()
exit()