Exemplo n.º 1
0
def worker(parentpid, shmkey, shmlock):
    configure_logging()
    logger = logging.getLogger('worker')

    logger.info("Worker has been started with pid %d", os.getpid())

    shm = sysv_ipc.SharedMemory(shmkey, flags=0, mode=0o600, size=0)
    try:
        signal.signal(signal.SIGINT, signal.SIG_IGN)
        while 1:
            try:
                if shmlock.acquire(block=True, timeout=1):
                    try:
                        data = Int2Int.from_ptr(shm.address)
                        logger.info("Processing %d keys: %s", len(data),
                                    list(data))
                    finally:
                        shmlock.release()
                else:
                    raise logger.warning("SHM is not available")
            except Exception:
                logger.exception("Processing data error")

            for unused in range(random.randint(0, 10)):
                if os.getppid() != parentpid:
                    raise StopProcess
                time.sleep(0.5)
    except StopProcess:
        logger.info("Stopping worker process")
    finally:
        shm.detach()

    sys.exit(0)
Exemplo n.º 2
0
def test_int2int_from_ptr(int2int_map):
    int2int_map[1] = 101
    int2int_map[2] = 102

    addr = int2int_map.buffer_ptr
    int2int_new_map = Int2Int.from_ptr(addr)

    assert int2int_new_map[1] == 101
    assert int2int_new_map[2] == 102