Python multiprocessing.connection.Connection() Examples

The following are 30 code examples of multiprocessing.connection.Connection(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module multiprocessing.connection , or try the search function .
Example #1
Source File: PlutoSDR.py    From urh with GNU General Public License v3.0 6 votes vote down vote up
def setup_device(cls, ctrl_connection: Connection, device_identifier):
        device_identifier = device_identifier if isinstance(device_identifier, str) else ""
        try:
            device_identifier = re.search("(?<=\[).+?(?=\])", device_identifier).group(0)
        except (IndexError, AttributeError):
            pass

        if not device_identifier:
            _, uris = plutosdr.scan_devices()
            try:
                device_identifier = uris[0]
            except IndexError:
                ctrl_connection.send("Could not find a connected PlutoSDR")
                return False

        ret = plutosdr.open(device_identifier)
        ctrl_connection.send("OPEN ({}):{}".format(device_identifier, ret))
        return ret == 0 
Example #2
Source File: LimeSDR.py    From urh with GNU General Public License v3.0 6 votes vote down vote up
def init_device(cls, ctrl_connection: Connection, is_tx: bool, parameters: OrderedDict):
        if not cls.setup_device(ctrl_connection, device_identifier=parameters["identifier"]):
            return False

        limesdr.enable_channel(True, is_tx, parameters[cls.Command.SET_CHANNEL_INDEX.name])
        limesdr.set_tx(is_tx)

        for parameter, value in parameters.items():
            cls.process_command((parameter, value), ctrl_connection, is_tx)

        antennas = limesdr.get_antenna_list()
        ctrl_connection.send("Current normalized gain is {0:.2f}".format(limesdr.get_normalized_gain()))
        ctrl_connection.send("Current antenna is {0}".format(antennas[limesdr.get_antenna()]))
        ctrl_connection.send("Current chip temperature is {0:.2f}°C".format(limesdr.get_chip_temperature()))

        return True 
Example #3
Source File: Device.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def receive_sync(cls, data_conn: Connection):
        raise NotImplementedError("Overwrite this method in subclass!") 
Example #4
Source File: USRP.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def receive_sync(cls, data_conn: Connection):
        usrp.recv_stream(data_conn, cls.SYNC_RX_CHUNK_SIZE) 
Example #5
Source File: USRP.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def prepare_sync_send(cls, ctrl_connection: Connection):
        ctrl_connection.send("Initializing stream...")
        usrp.setup_stream()
        ret = usrp.start_stream(0)
        ctrl_connection.send("Initialize stream:{0}".format(ret))
        return ret 
Example #6
Source File: BladeRF.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def init_device(cls, ctrl_connection: Connection, is_tx: bool, parameters: OrderedDict) -> bool:
        bladerf.set_tx(is_tx)
        return super().init_device(ctrl_connection, is_tx, parameters) 
Example #7
Source File: BladeRF.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def prepare_sync_receive(cls, ctrl_connection: Connection):
        ctrl_connection.send("Initializing BladeRF..")
        ret = bladerf.prepare_sync()
        return ret 
Example #8
Source File: BladeRF.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def receive_sync(cls, data_conn: Connection):
        bladerf.receive_sync(data_conn, cls.SYNC_RX_CHUNK_SIZE) 
Example #9
Source File: BladeRF.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def prepare_sync_send(cls, ctrl_connection: Connection):
        ctrl_connection.send("Initializing BladeRF...")
        ret = bladerf.prepare_sync()
        return ret 
Example #10
Source File: Device.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def setup_device(cls, ctrl_connection: Connection, device_identifier):
        raise NotImplementedError("Overwrite this method in subclass!") 
Example #11
Source File: Device.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def enter_async_receive_mode(cls, data_connection: Connection, ctrl_connection: Connection) -> int:
        raise NotImplementedError("Overwrite this method in subclass!") 
Example #12
Source File: Device.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def prepare_sync_receive(cls, ctrl_connection: Connection):
        raise NotImplementedError("Overwrite this method in subclass!") 
Example #13
Source File: USRP.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def init_device(cls, ctrl_connection: Connection, is_tx: bool, parameters: OrderedDict):
        usrp.set_tx(is_tx)
        success = super().init_device(ctrl_connection, is_tx, parameters)
        if success:
            ctrl_connection.send("Current antenna is {} (possible antennas: {})".format(usrp.get_antenna(),
                                                                                        ", ".join(usrp.get_antennas())))
        return success 
Example #14
Source File: Device.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def prepare_sync_send(cls, ctrl_connection: Connection):
        raise NotImplementedError("Overwrite this method in subclass!") 
Example #15
Source File: Device.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def device_receive(cls, data_connection: Connection, ctrl_connection: Connection, dev_parameters: OrderedDict):
        if not cls.init_device(ctrl_connection, is_tx=False, parameters=dev_parameters):
            ctrl_connection.send("failed to start rx mode")
            return False

        try:
            cls.adapt_num_read_samples_to_sample_rate(dev_parameters[cls.Command.SET_SAMPLE_RATE.name])
        except NotImplementedError:
            # Many SDRs like HackRF or AirSpy do not need to calculate SYNC_RX_CHUNK_SIZE
            # as default values are either fine or given by the hardware
            pass

        if cls.ASYNCHRONOUS:
            ret = cls.enter_async_receive_mode(data_connection, ctrl_connection)
        else:
            ret = cls.prepare_sync_receive(ctrl_connection)

        if ret != 0:
            ctrl_connection.send("failed to start rx mode")
            return False

        exit_requested = False
        ctrl_connection.send("successfully started rx mode")

        while not exit_requested:
            if cls.ASYNCHRONOUS:
                try:
                    time.sleep(0.25)
                except KeyboardInterrupt:
                    pass
            else:
                cls.receive_sync(data_connection)
            while ctrl_connection.poll():
                result = cls.process_command(ctrl_connection.recv(), ctrl_connection, is_tx=False)
                if result == cls.Command.STOP.name:
                    exit_requested = True
                    break

        cls.shutdown_device(ctrl_connection, is_tx=False)
        data_connection.close()
        ctrl_connection.close() 
Example #16
Source File: AirSpy.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def setup_device(cls, ctrl_connection: Connection, device_identifier):
        ret = airspy.open()
        ctrl_connection.send("OPEN:" + str(ret))
        return ret == 0 
Example #17
Source File: AirSpy.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def enter_async_receive_mode(cls, data_connection: Connection, ctrl_connection: Connection):
        ret = airspy.start_rx(data_connection.send_bytes)
        ctrl_connection.send("Start RX MODE:" + str(ret))
        return ret 
Example #18
Source File: SoundCard.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def init_device(cls, ctrl_connection: Connection, is_tx: bool, parameters: OrderedDict) -> bool:
        try:
            cls.SAMPLE_RATE = int(parameters[cls.Command.SET_SAMPLE_RATE.name])
        except (KeyError, ValueError):
            pass
        return super().init_device(ctrl_connection, is_tx, parameters) 
Example #19
Source File: SoundCard.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def setup_device(cls, ctrl_connection: Connection, device_identifier):
        ctrl_connection.send("Initializing pyaudio...")
        try:
            cls.pyaudio_handle = pyaudio.PyAudio()
            ctrl_connection.send("Initialized pyaudio")
            return True
        except Exception as e:
            logger.exception(e)
            ctrl_connection.send("Failed to initialize pyaudio") 
Example #20
Source File: SoundCard.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def prepare_sync_receive(cls, ctrl_connection: Connection):
        try:
            cls.pyaudio_stream = cls.pyaudio_handle.open(format=pyaudio.paFloat32,
                                                         channels=2,
                                                         rate=cls.SAMPLE_RATE,
                                                         input=True,
                                                         frames_per_buffer=cls.CHUNK_SIZE)
            ctrl_connection.send("Successfully started pyaudio stream")
            return 0
        except Exception as e:
            logger.exception(e)
            ctrl_connection.send("Failed to start pyaudio stream") 
Example #21
Source File: SoundCard.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def receive_sync(cls, data_conn: Connection):
        if cls.pyaudio_stream:
            data_conn.send_bytes(cls.pyaudio_stream.read(cls.CHUNK_SIZE, exception_on_overflow=False)) 
Example #22
Source File: TestSDRPlay.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def recv(conn: Connection):
    while True:
        t = time.time()
        result = SDRPlay.bytes_to_iq(conn.recv_bytes())
        print("UNPACK", time.time()-t) 
Example #23
Source File: live_spectrogram.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def generate_data(connection: Connection, num_samples=32768):
    frequency = 0.1
    divisor = 200
    pos = 0
    while True:
        result = np.zeros(num_samples, dtype=np.complex64)
        result.real = np.cos(2 * np.pi * frequency * np.arange(pos, pos + num_samples))
        result.imag = np.sin(2 * np.pi * frequency * np.arange(pos, pos + num_samples))
        pos += num_samples
        if pos / num_samples >= divisor:
            frequency *= 2
            if frequency >= 1:
                frequency = 0.1
            pos = 0
        connection.send(result) 
Example #24
Source File: similar.py    From Greynir with GNU General Public License v3.0 5 votes vote down vote up
def _SocketClient(address):
    """ Return a connection object connected to the socket given by `address` """
    with closing(socket.socket(socket.AF_INET)) as s:
        s.setblocking(True)
        s.connect(address)
        return Connection(s.detach()) 
Example #25
Source File: HackRF.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def shutdown_device(cls, ctrl_conn: Connection, is_tx: bool):
        if is_tx:
            result = hackrf.stop_tx_mode()
            ctrl_conn.send("STOP TX MODE:" + str(result))
        else:
            result = hackrf.stop_rx_mode()
            ctrl_conn.send("STOP RX MODE:" + str(result))

        result = hackrf.close()
        ctrl_conn.send("CLOSE:" + str(result))

        result = hackrf.exit()
        ctrl_conn.send("EXIT:" + str(result))

        return True 
Example #26
Source File: augmenter.py    From delira with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, dataloader: DataLoader,
                 output_pipe: mpconnection.Connection,
                 index_pipe: mpconnection.Connection,
                 abort_event: multiprocessing.Event,
                 transforms: Callable,
                 process_id):
        """
        Parameters
        ----------
        dataloader : :class:`DataLoader`
            the data loader which loads the data corresponding to the given
            indices
        output_pipe : :class:`multiprocessing.connection.Connection`
            the pipe, the loaded data shoud be sent to
        index_pipe : :class:`multiprocessing.connection.Connection`
            the pipe to accept the indices
        abort_event : class:`multiprocessing.Event`
            the abortion event; will be set for every Exception;
            If set: Worker terminates
        transforms : :class:`collections.Callable`
            the transforms to transform the data
        process_id : int
            the process id
        """
        super().__init__()

        self._data_loader = dataloader
        self._output_pipe = output_pipe
        self._input_pipe = index_pipe
        self._abort_event = abort_event
        self._process_id = process_id
        self._transforms = transforms 
Example #27
Source File: _posix_reduction.py    From loky with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def rebuild_connection(df, readable, writable):
        fd = df.detach()
        return Connection(fd, readable, writable) 
Example #28
Source File: test_iso_packager.py    From randovania with GNU General Public License v3.0 5 votes vote down vote up
def test_shared_process_code_success():
    # Setup
    mock_target = MagicMock()
    iso = MagicMock()
    game_files_path = MagicMock()
    on_finish_message = MagicMock()
    progress_update = MagicMock()

    process = MagicMock()

    def process_effect(target, args):
        output_pipe: connection.Connection = args[0]
        output_pipe.send((False, "Message 1", 0.2))
        output_pipe.send((False, "Message 2", 0.5))
        output_pipe.send((True, None, 100))
        return process

    # Run
    with patch("multiprocessing.Process", side_effect=process_effect,
               autospec=work_around_pytest_qt_bug) as mock_process:
        iso_packager._shared_process_code(mock_target, iso, game_files_path, on_finish_message, progress_update)

    mock_process.assert_called_once_with(target=mock_target, args=(ANY, iso, game_files_path))
    process.start.assert_called_once_with()
    process.terminate.assert_called_once_with()
    progress_update.assert_has_calls([
        call("", 0),
        call("Message 1", 0.2),
        call("Message 2", 0.5),
        call(on_finish_message, 1),
    ]) 
Example #29
Source File: test_iso_packager.py    From randovania with GNU General Public License v3.0 5 votes vote down vote up
def test_shared_process_code_failure():
    # Setup
    mock_target = MagicMock()
    iso = MagicMock()
    game_files_path = MagicMock()
    on_finish_message = MagicMock()
    progress_update = MagicMock()

    process = MagicMock()

    def process_effect(target, args):
        output_pipe: connection.Connection = args[0]
        output_pipe.send((False, "Message 1", 0.2))
        output_pipe.send((True, "You got an error!", 100))
        return process

    # Run
    with patch("multiprocessing.Process", side_effect=process_effect,
               autospec=work_around_pytest_qt_bug) as mock_process:
        with pytest.raises(RuntimeError) as exception:
            iso_packager._shared_process_code(mock_target, iso, game_files_path, on_finish_message, progress_update)

    mock_process.assert_called_once_with(target=mock_target, args=(ANY, iso, game_files_path))
    process.start.assert_called_once_with()
    process.terminate.assert_called_once_with()
    progress_update.assert_has_calls([
        call("", 0),
        call("Message 1", 0.2),
    ])
    assert str(exception.value) == "You got an error!" 
Example #30
Source File: test_get_cache_backend.py    From FlowKit with Mozilla Public License 2.0 5 votes vote down vote up
def read_key(lock: Lock, connection: Connection):
    cache = get_cache_backend()
    with lock:
        connection.send(cache.get("TEST_KEY"))