<commit_before>#!/usr/bin/env python


import threading
import socket
import time

import libardrone
import video

class ARDroneVideoThread(threading.Thread):

    def run(self):
        print 'preparing server...'
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.settimeout(1)
        sock.bind(('', 5555))
        self.stopping = False
        # will the drone send one frame per udp package?

        print 'sending wakeup'
        sock.sendto("\x01\x00\x00\x00", ('192.168.1.1', 5555))

        print 'waiting for incoming data'
        while not self.stopping:
            print 'reading socket...',
            data, address = sock.recvfrom(65535)    # max udp packet size

            br = video.BitReader(data)
            video.read_picture(br)
            print 'ok.'
            # do something with the data
            print time.time(), len(data)
        print 'left receiving loop'
        socket.close()

    def stop(self):
        print "signaling thread to stop"
        self.stopping = True

def main():
    vthread = ARDroneVideoThread()
    vthread.start()
    time.sleep(60)
    vthread.stop()


if __name__ == '__main__':
    main()
<commit_msg>Use select and added option to break the loop by hitting enter.<commit_after>#!/usr/bin/env python

import datetime
import select
import socket
import sys
import threading
import time

import libardrone
import video

class ARDroneVideoThread(threading.Thread):

    def run(self):
        print 'preparing server...'
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.setblocking(0)
        sock.bind(('', 5555))
        self.stopping = False
        # will the drone send one frame per udp package?

        print 'sending wakeup'
        sock.sendto("\x01\x00\x00\x00", ('192.168.1.1', 5555))

        print 'waiting for incoming data'
        j = 0
        while not self.stopping:
            j += 1
            inputready, outputready, exceptready = select.select([sock, sys.stdin], [], [])
            for i in inputready:
                if i == sock:
                    print 'reading socket...',
                    t = datetime.datetime.now()
                    while 1:
                        try:
                            j += 1
                            data, address = sock.recvfrom(65535)    # max udp packet size
                        except:
                            print "dropped", j-1, "frames"
                            j = 0
                            break
                    br = video.BitReader(data)
                    t_decode = video.read_picture(br)
                    t2 = datetime.datetime.now()
                    print 'ok.', (t2 - t).microseconds / 1000000., 'sec', t_decode
                elif i == sys.stdin:
                    sys.stdin.readline()
                    print "stopping the loop"
                    self.stopping = True
        print 'left receiving loop'
        sock.close()

    def stop(self):
        print "signaling thread to stop"
        self.stopping = True

def main():
    vthread = ARDroneVideoThread()
    vthread.start()
    #time.sleep(60)
    #vthread.stop()
    vthread.join()

if __name__ == '__main__':
    main()

{'fraction_non_alphanumeric': 0.10123839009287926, 'fraction_numerical': 0.024458204334365324, 'mean_word_length': 1.6161943319838057, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3243506', 'n_tokens_mistral': 988, 'n_tokens_neox': 896, 'n_words': 255}