Beispiel #1
0
        def _recv_loop(self):
            from select import select
            from time import sleep

            while self.socket.fileno() != -1:  # Socket is live
                try:
                    if len(select([self.socket], [], [],
                                  0)[0]) == 1:  # Readable
                        break
                except ValueError:  # Socket is dead
                    self.disconnect()
                    return

                if self._disconnected:
                    return

                sleep(1e-9)

            while True:
                try:
                    buffer = self.socket.recv(4096)
                except:  # Peer disconnected?
                    self.disconnect()
                    return

                if len(buffer) > 0:
                    pf.thread(lambda: self._stream.on_receive(self, buffer))
                else:  # Peer disconnected
                    self.disconnect()
                    return
Beispiel #2
0
    def __init__(self,
                 wavedata,
                 bitrate=None,
                 duration=None,
                 num_channels=1,
                 bit_depth=16,
                 block=False):
        import pyformulas as pf

        self.wavedata = wavedata if (isinstance(wavedata, bytes) or isinstance(
            wavedata, bytearray)) else bytes(wavedata)
        self.bitrate = bitrate
        self.duration = duration
        self.num_channels = num_channels

        formats = {
            8: pyaudio.paInt8,
            16: pyaudio.paInt16,
            24: pyaudio.paInt24,
            32: pyaudio.paInt32
        }
        self.format = formats[bit_depth]

        assert (len(wavedata) % (bit_depth / 8 * num_channels)) == 0

        if block:
            self._play()
        else:
            pf.thread(self._play)
Beispiel #3
0
    def _make_server(self, port, bind_address, max_connections):
        import socket
        self._serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        self._serversocket.bind((bind_address, port))
        self._serversocket.listen(max_connections)

        for i in range(max_connections):
            pf.thread(self._server_accept)
Beispiel #4
0
        def __init__(self, stream, socket):
            from select import select
            from time import sleep
            self._select = select
            self._sleep = sleep

            self._stream = stream
            self.socket = socket

            self._disconnected = False

            pf.thread(self._recv_loop)

            self._stream.on_connect(self)
Beispiel #5
0
        def disconnect(self):
            if self._disconnected:
                return

            self._disconnected = True

            try:
                self.socket.close()
            except:
                pass

            self._stream._connections.remove(self)

            if self._stream._isserver:  # Listen for another connection
                if self._stream._serversocket.fileno() != -1:
                    pf.thread(self._stream._server_accept)

            self._stream.on_disconnect(self)
Beispiel #6
0
"""
Basic multithreading with pf.thread
"""

import pyformulas as pf
import time


def count_to(num):
    for i in range(1, num + 1):
        print("%i..." % i)

        time.sleep(0.5)


thread = pf.thread(count_to, (10, ))

time.sleep(2.5)
print("foo")
thread.join()
print('Done')