예제 #1
0
파일: proc.py 프로젝트: LeoKudrik/ttask
    def run(self):
        queue = MessageQueue(config.POSIX_Q_NAME, O_CREAT,
                             max_message_size=config.POSIX_Q_MAXSIZE)

        def cleaner(a, b):
            # закрываем posix queue тут
            try:
                queue.close()
            except:
                pass

        atexit.register(cleaner)
        signal.signal(signal.SIGTERM, cleaner)

        while True:
            try:
                # ждём чего-нибудь от джанги здесь
                (msg, _) = queue.receive()
            except SignalError:
                exit()
            # отделяем зёрна от плевел
            action = msg[:1]  # 'd' - удаление, 'a' - добавление
            url = eval(msg[1:])  # а тут сами данные; делаем их питонячими
            if action == 'd':
                # оказывается, произошло удаление модели урла, удалить и
                # из очереди LoopProc.queue
                self._pipe.send(['remove', (0, url)])
                continue
            if url['state'] == Url.WAIT and action == 'a':
                # кидаем в пайп QueueManager-у урл на добавление в работу
                self._pipe.send(['add', self._calculate_time(url)])
예제 #2
0
class SpotGyro():
    def __init__(self):
        try:
            unlink_message_queue("/gyro")
        except:
            pass

        self.mqueue = MessageQueue('/gyro', flags=O_CREX, max_messages=MAX_MSG)

        try:
            self.gyro = mpu6050(0x68)
        except:
            raise Exception("Could not connect to Gyroscope MPU6050")

        self.angleFront = 0.
        self.angleSide = 0.

    def get_position(self):

        try:
            accelval = self.gyro.get_accel_data()
            gyroval = self.gyro.get_gyro_data()

            self.angleFront = 0.80 * (
                self.angleFront + float(gyroval['y']) * 0.01 / 131
            ) + 0.20 * atan2(accelval['x'], accelval['z']) * 180 / 3.14159
            self.angleSide = 0.80 * (
                self.angleSide + float(gyroval['x']) * 0.01 / 131
            ) + 0.20 * atan2(accelval['y'], accelval['z']) * 180 / 3.14159

            if self.mqueue.current_messages >= MAX_MSG:
                self.mqueue.receive()

            self.mqueue.send('{"front":"' + str(self.angleFront) +
                             '","side":"' + str(self.angleSide) +
                             '","time":"' + str(time()) + '"}')

        except Exception:
            pass
예제 #3
0
    def read_window_list_from_queue(stop, window):
        mq = MessageQueue("/lcarswm-active-window-list")
        while True:
            try:
                s, _ = mq.receive(.4)
                GLib.idle_add(window.on_list_update, window, s.decode("utf-8"))
            except BusyError:
                pass

            if stop():
                break

        mq.close()
예제 #4
0
def pi(n):
    pids = []
    unit = n / 10
    q = Queue("/pi", flags=os.O_CREAT)
    for i in range(10):
        mink = unit * i
        maxk = mink + unit
        pid = os.fork()
        if pid > 0:
            pids.append(pid)
        else:
            s = slice(mink, maxk)
            q.send(str(s))
            q.close()
            sys.exit(0)
    sums = []
    for pid in pids:
        sums.append(float(q.receive()[0]))
        os.waitpid(pid, 0)
    q.close()
    q.unlink()
    return math.sqrt(sum(sums) * 8)
예제 #5
0
class SpotGamepad():
    def __init__(self):
        try:
            unlink_message_queue("/spotgamepad")
            unlink_message_queue("/cameramotion")
        except:
            pass

        self.motionqueue = MessageQueue('/spotgamepad',
                                        flags=O_CREX,
                                        max_messages=MAX_MSG)
        self.cameraqueue = MessageQueue('/cameramotion',
                                        flags=O_CREX,
                                        max_messages=MAX_MSG)

        # Initialize Gamepad
        self.gamepad = evdev.InputDevice(INPUT_DEVICE)

    def read_event(self):
        for event in self.gamepad.read_loop():
            if event.type == evdev.ecodes.EV_ABS:
                absevent = evdev.categorize(event)

                # If motion button
                if evdev.ecodes.bytype[absevent.event.type][
                        absevent.event.code] == 'ABS_HAT0Y':
                    code = absevent.event.value

                    if code == -1:
                        self.send_msg(
                            'spotgamepad', '{"action":"forward","time":"' +
                            str(time()) + '"}')
                    elif code == 1:
                        self.send_msg(
                            'spotgamepad', '{"action":"backward","time":"' +
                            str(time()) + '"}')
                    else:
                        self.send_msg(
                            'spotgamepad',
                            '{"action":"stop","time":"' + str(time()) + '"}')

                if evdev.ecodes.bytype[absevent.event.type][
                        absevent.event.code] == 'ABS_HAT0X':
                    code = absevent.event.value

                    if code == -1:
                        self.send_msg(
                            'spotgamepad',
                            '{"action":"left","time":"' + str(time()) + '"}')
                    elif code == 1:
                        self.send_msg(
                            'spotgamepad',
                            '{"action":"right","time":"' + str(time()) + '"}')
                    else:
                        self.send_msg(
                            'spotgamepad',
                            '{"action":"stop","time":"' + str(time()) + '"}')

                # If body position joystick
                if evdev.ecodes.bytype[absevent.event.type][
                        absevent.event.code] == 'ABS_X':
                    if absevent.event.value < 128:
                        self.send_msg(
                            'spotgamepad', '{"action":"bodyleft","time":"' +
                            str(time()) + '"}')
                    elif absevent.event.value > 128:
                        self.send_msg(
                            'spotgamepad', '{"action":"bodyright","time":"' +
                            str(time()) + '"}')
                    else:
                        self.send_msg(
                            'spotgamepad',
                            '{"action":"stop","time":"' + str(time()) + '"}')

                if evdev.ecodes.bytype[absevent.event.type][
                        absevent.event.code] == 'ABS_Y':
                    if absevent.event.value < 128:
                        self.send_msg(
                            'spotgamepad', '{"action":"bodyfront","time":"' +
                            str(time()) + '"}')
                    elif absevent.event.value > 128:
                        self.send_msg(
                            'spotgamepad', '{"action":"bodyback","time":"' +
                            str(time()) + '"}')
                    else:
                        self.send_msg(
                            'spotgamepad',
                            '{"action":"stop","time":"' + str(time()) + '"}')

                # If camera position joystick
                if evdev.ecodes.bytype[absevent.event.type][
                        absevent.event.code] == 'ABS_Z':
                    print('lateral camera: ' + str(absevent.event.value))

                if evdev.ecodes.bytype[absevent.event.type][
                        absevent.event.code] == 'ABS_RZ':
                    print('front camera: ' + str(absevent.event.value))

            # If action button
            if event.type == evdev.ecodes.EV_KEY:
                keyevent = evdev.categorize(event)

                # Laydown
                if laydown_button in evdev.ecodes.bytype[keyevent.event.type][
                        keyevent.event.code]:
                    if keyevent.event.value == 0:
                        self.send_msg(
                            'spotgamepad', '{"action":"laydown","time":"' +
                            str(time()) + '"}')

                # Wake up
                if wakeup_button in evdev.ecodes.bytype[keyevent.event.type][
                        keyevent.event.code]:
                    if keyevent.event.value == 0:
                        self.send_msg(
                            'spotgamepad',
                            '{"action":"wakeup","time":"' + str(time()) + '"}')

    def send_msg(self, queue, msg):

        if self.cameraqueue.current_messages >= MAX_MSG:
            self.cameraqueue.receive()

        if self.motionqueue.current_messages >= MAX_MSG:
            self.motionqueue.receive()

        if queue == 'spotgamepad':
            self.motionqueue.send(msg)

        elif queue == 'cameramotion':
            self.cameraqueue.send(msg)
예제 #6
0
class message_queue:
    O_CREAT = threading.O_CREAT
    O_RDONLY = threading.O_RDONLY

    def __init__(self, mq_name, mode=O_CREAT):
        log_format = '%(asctime)-15s : %(levelname)s : line %(lineno)-4d in %(module)s.%(funcName)s   > %(message)s'
        logging.basicConfig(level=logging.DEBUG, format=log_format)
        self.logger = logging.getLogger('mq-mq_name')

        self.name = mq_name
        self.lock = threading.Lock()
        self.valid = False
        self.delete_on_destruct = False

        if mode is message_queue.O_CREAT:
            self.create_mq()

        else:  # RD_ONLY but need verify for RDWR and WR_ONLY
            self.connect_mq()

        return

    def create_mq(self):
        self.delete_on_destruct = True
        self.logger.info('Attempting to create messsage_queue: ' + self.name)
        try:
            self.mq = MessageQueue(self.name, MessageQueue.O_CREAT)
            self.valid = True

        except:
            self.mq = None
            e = sys.exc_info()[0]
            self.logger.info('Error creating message_queue:' + e.__repr__())
            return None

    def connect_mq(self):
        self.delete_on_destruct = False
        self.logger.info('Attemping to connect to message_queue:' + self.name)
        try:
            MessageQueue(self.name, threading.O_RDONLY)
            self.valid = True
        except ExistentialError as e:
            self.logger.info(' - Could not connect to message_queue::' + e.__repr__())
            self.mq = None

    def send(self, message):
        if not self.valid:
            self.logger.info('Cannot send message:' + self.name + 'is invalid.')
        try:
            self.mq.send(message)
        except:
            e = sys.exc_info()[0]
            self.logger.info('Error sending message:' + e.__repr__())

    def receive(self, timeout=0):
        if not self.valid:
            self.logger.info('Cannot receive messages:' + self.name + 'is invalid.')
            return

        try:
            message = self.mq.receive(timeout)
            return message
        except:
            e = sys.exc_info()[0]
            self.logger.info('Error receiving message:' + e.__repr__())
            return None

    def delete_when_finished(self, delete_when_finished):
        # probably should test type for bool_type
        self.delete_on_destruct = delete_when_finished

    def __enter__(self):
        return

    def __exit__(self):
        if not self.delete_on_destruct
            return

        try:
            self.logger.info('Unlinking MessageQueue:' + self.name)
            unlink_message_queue(self.name)
        except ExistentialError as e:
            pass
예제 #7
0
    def run(self):  # overrides threading.Thread.run()
        """
        Overrides threading.Thread.run()
        Creates initial connection to expected message queues; echometrics, frames, and tracks.  It polls each one
        waiting for the read-ready flag to be set and consumes the data.  It reformats the data into an object and
        writes it out to clients who are registered.

        Returns
        -------
        None
        """

        logger.info('Creating message_queues: ' + self.display_q_name)
        display_q = MessageQueue(self.display_q_name, O_CREAT)
        em_q = MessageQueue(self.em_q_name, O_CREAT)

        # TODO:  READ THE NIMS FRAMEBUFFER NAME FROM THE YAML
        try:  # connect to nims ingestor to get backscatter data
            nims_framebuffer = '/' + self.config['FRAMEBUFFER_NAME']
            logger.info('Connecting to ' + nims_framebuffer)
            framebuffer_q = MessageQueue(nims_framebuffer, O_RDONLY)
            framebuffer_q.send(self.display_q_name)
            logger.info(" - sent queue: " + self.display_q_name)
        except ExistentialError as e:
            logger.info(' - Could not connect to ' + nims_framebuffer + '::' +
                        e.__repr__())

        try:  # connect to nims tracker to get track data (probably out of sync)
            tracker_name = '/' + self.config['TRACKER_NAME']
            logger.info('Connecting to ' + tracker_name)
            trackbuffer_q = MessageQueue(tracker_name, O_RDONLY)
        except ExistentialError as e:
            logger.info(' - Could not connect to ' + tracker_name + '::' +
                        e.__repr__())

        try:  # connect to the echometrics queue for periodic em data
            em_queue_name = self.config['ECHOMETRICS']['queue_name']

            logger.info('Connecting to ' + em_queue_name)
            em_request_q = MessageQueue(em_queue_name, O_RDONLY)
            em_request_q.send(self.em_q_name)
        except:
            logger.info(' - Could not connect to ' + em_queue_name)

        poller = select.poll()
        poller.register(em_q.mqd, select.POLLIN)
        poller.register(display_q.mqd, select.POLLIN)
        poller.register(trackbuffer_q.mqd, select.POLLIN)

        time.sleep(
            1
        )  # apparently necessary to create this latency for the frame_buffer app?

        while True:
            frame_buffer = None
            track_buffer = None
            em_buffer = None

            mq_state = poller.poll()
            for state in mq_state:
                mqd = state[0]
                if mqd == em_q.mqd:
                    buf = em_q.receive()[0]
                    em_buffer = ast.literal_eval(buf)
                elif mqd == display_q.mqd:
                    frame = frames.frame_message(display_q.receive()[0])
                    if frame.valid is False:
                        logger.info('Received invalid message: ignoring')
                        continue
                    try:
                        #logger.info(' -- Connecting to ' + frame.shm_location)
                        shm_frame = SharedMemory(frame.shm_location,
                                                 O_RDONLY,
                                                 size=frame.frame_length)
                    except StandardError as e:
                        logger.info(' -- Error connecting to',
                                    frame.shm_location, '::', e.__repr__())
                        continue
                    mapped = mmap(shm_frame.fd, shm_frame.size)
                    shm_frame.close_fd()
                    frame_buffer = frames.frame_buffer(
                        mapped.read(frame.frame_length))
                    mapped.close()
                    if frame_buffer.valid is False:
                        logger.info(' -- Error Parsing Frame')
                        continue

                    image = np.array(frame_buffer.image)
                    image = image.reshape((frame_buffer.num_samples[0],
                                           frame_buffer.num_beams[0]))
                    image = image[0:-1:4, ::]
                    frame_buffer.image = image.flatten().tolist()
                    frame_buffer.num_samples = (frame_buffer.num_samples[0] /
                                                4, 0)

                elif mqd == trackbuffer_q.mqd:
                    track_buffer = frames.track_message(
                        trackbuffer_q.receive()[0])

            clients = copy.copy(self.clients)
            for client in clients:
                try:
                    if frame_buffer:
                        client.send_image(frame_buffer)
                    if track_buffer:
                        client.send_tracks(track_buffer)
                    if em_buffer:
                        client.send_metrics(em_buffer)

                except StandardError as e:
                    logger.info("Error sending data to client")
                    print sys.exc_info()
                    continue

        return
예제 #8
0
from posix_ipc import MessageQueue as MsgQ
import sysv_ipc
import os

if __name__ == '__main__':
    q = MsgQ("/tmp", flags=os.O_CREAT, mode=0666)
    q.send("adfg")
    rec = q.receive(1)
    print rec
    q.close()
    q.unlink()

    q2 = sysv_ipc.MessageQueue(16842753, flags=sysv_ipc.IPC_CREAT)
    q2.send("sferogh", type=1)