Exemplo n.º 1
0
async def main_run(qFromGui: queue, qToGui: queue) -> None:
    ''' main worker thread uses q's to talk to gui'''
    print("Its main worker.")
    files = classFiles.classFiles()
    qToGui.put(("lb1", f"Its main worker. START"))
    dirPrimary = os.path.expanduser("~/Pictures/Photos")
    asyncio.ensure_future(
        readHashFromFile(
            files=files,
            filename=dirPrimary + "/hashdeep/20210321-hashdeep.hash.txt",
        ))
    asyncio.ensure_future(
        run_file_compare(dir=dirPrimary,
                         files=files,
                         qLog=(qToGui, "lb2"),
                         qInfo=(qToGui, "TextInfo1")))
    while not globals.exitFlag:
        try:
            event, values = qFromGui.get_nowait(
            )  # see if something has been posted to Queue
        except queue.Empty:  # get_nowait() will get exception when Queue is empty
            event = None  # nothing in queue so do nothing
            await asyncio.sleep(1)
        if event:
            print(
                f"photoDirSync.py main: qFromGui {event=} {values=} {globals.exitFlag=}"
            )
            if event == "SaveImg":
                qToGui.put(("lb1", f"Got [SaveImg] event in photodirSync.py"))

    print("The end. main().")
Exemplo n.º 2
0
def put_random_int(q: queue):
    print("Start put_random_int")
    nombre_production = 10
    cpt = 0
    while cpt < nombre_production:
        q.put(random.randint(0, 20))
        cpt = cpt + 1
    print("End put_random_int")
Exemplo n.º 3
0
def thread_data(qu: queue, urls: tuple) -> None:
    """
    for one thread target func
    :param qu: queue for getting data
    :param urls: tuple of urls
    """

    for url in urls:
        qu.put(parse(url))
Exemplo n.º 4
0
 def execute_moves(self, excpt_move=""):
     moves = Queue()
     _ = self.state.percept()
     for move in self.state.action:
         if move != excpt_move:
             nod = deepcopy(self.state)
             nod.execute_action(move)
             moves.put(Node(nod, self, move))
     return moves
Exemplo n.º 5
0
 def search(self):
     closed = []
     child = Queue()
     child.put(self.start)
     correct = []
     while True:
         if child.empty(): return max(correct), self.step
         actual = child.get()
         if self.chck == True:
             if child.empty(): return max(correct), self.step
             correct.append(actual.state.total_correct())
             actual = child.get()
         elif actual.state.check():
             return actual.state.total_correct(), self.step
         elif actual.state.list_things_at not in closed:
             closed.append(actual.state.list_things_at)
             if actual.depth > 1:
                 exec = actual.execute_moves(actual.action.split()[-2])
             else:
                 exec = actual.execute_moves()
             while not exec.empty():
                 if self.step >= self.give_step:
                     self.chck = True
                 else:
                     self.step += 1
                 child.put(exec.get())
Exemplo n.º 6
0
def run(files: list, result_queue: queue) -> None:
    for file in files:
        try:
            global count_of_points_in_index
            face_descriptor = get_face_descriptor('users/' + file)
            embedding = np.array(face_descriptor)
            result_queue.put_nowait(
                str(count_of_points_in_index) + '|' + file + '\n')
            index.addDataPoint(count_of_points_in_index, embedding)
            count_of_points_in_index += 1
            print(count_of_points_in_index)
        except Exception as ex:
            print(ex)
Exemplo n.º 7
0
def add_neighbors_to_Q(Q: queue, dict_of_items_curr_agent_share: dict) -> None:
    """
    >>> agent1= AdditiveAgent({"a": 100, "b": 10, "c": 50, "d": 100 ,"e": 70,"f": 100, "g": 300, "h": 40, "i": 30}, name="agent1")
    >>> agent3= AdditiveAgent({"a": 10, "b": 30, "c": 30, "d": 40 ,"e": 180,"f": 100, "g": 300, "h": 20, "i": 90}, name="agent3")
    >>> d={'f': [agent1 , agent3]}
    >>> q = queue.Queue()
    >>> add_neighbors_to_Q(q, d)
    >>> print([a.name() for a in q.queue])
    ['agent1', 'agent3']
    """
    for agents in dict_of_items_curr_agent_share.values():
        for agent in agents:
            if agent not in Q.queue:
                Q.put(agent)
Exemplo n.º 8
0
def _main_process_write(out_queue: queue, output: _OutputWrapper,
                        parallel_level: int, abort: Event):
    cur = 0
    results = dict()
    terminating = 0
    while True:
        if abort.is_set():
            return
        try:
            msg, mini_batch_index, result = out_queue.get(timeout=0.1)
        except queue.Empty:
            continue

        if msg is not None:
            if msg == "terminating":
                terminating += 1
                if terminating == parallel_level:
                    return
                continue
            else:
                print(
                    f"Analysis failed in mini_batch #{mini_batch_index}. Stopping all the processes.",
                    file=sys.stderr)
                print(msg, file=sys.stderr)
                return

        # output must be ordered same as input text
        results[mini_batch_index] = result
        while results:
            if cur not in results.keys():
                break
            result = results[cur]
            del results[cur]
            cur += 1
            output.write(result)
Exemplo n.º 9
0
def get_random_int(q: queue):
    print("Start get_random_int")
    permission_de_prendre_un_cookie.acquire()
    cookie = q.get()
    print(f"j'ai pris ce cookie: {cookie}")
    permission_de_prendre_un_cookie.release()
    print("End get_random_int")
Exemplo n.º 10
0
    def __init__(self, stream):
        '''
        stream: the stream to read from.
                Usually a process' stdout or stderr.
        '''

        self._s = stream
        self._q = Queue()

        def _populateQueue(stream, queue):
            '''
            Collect lines from 'stream' and put them in 'queue'.
            '''

            #while True:
            while (self.end_of_stream == False):
                line = stream.readline()
                if line:
                    queue.put(line)
                    if (line.find("Compilation finished with errors!") >= 0
                            or line.find("Compilation finished successfully!")
                            >= 0):
                        self.end_of_stream = True
                else:
                    self.end_of_stream = True
                    raise UnexpectedEndOfStream

        self._t = Thread(target=_populateQueue, args=(self._s, self._q))
        self._t.daemon = True
        self._t.start()  #start collecting lines from the stream
    def breadth_first_search(self):
        q = Queue()
        q.put(self)

        while not q.empty():
            cur_node = q.get()
            print(cur_node.value)

            if cur_node.left_child:
                q.put(cur_node.left_child)

            if cur_node.right_child:
                q.put(cur_node.right_child)
Exemplo n.º 12
0
 def deserialize_helper(self, q: queue):
     val = q.get()
     if val == 'X':
         return None
     root = Node(int(val))
     root.left = self.deserialize_helper(q)
     root.right = self.deserialize_helper(q)
     return root
Exemplo n.º 13
0
    def validTree(self, n, edges):
        # Write your code here
        if len(edges) != n - 1:
            return False

        neighbors = collections.defaultdict(list)
        for u, v in edges:
            neighbors[u].append(v)
            neighbors[v].append(u)

        visited = {}
        #from Queue import Queue
        queue = Queue()

        queue.put(0)
        visited[0] = True
        while not queue.empty():
            cur = queue.get()
            visited[cur] = True
            for node in neighbors[cur]:
                if node not in visited:
                    visited[node] = True
                    queue.put(node)

        return len(visited) == n
Exemplo n.º 14
0
def multiProcess():
    num = splitFile()
    p_list = []
    queue = Queue()
    count = 0
    for i in range(num):
        p = Process(target=processTask, args=(i, queue))
        p_list.append(p)
    for i in p_list:
        i.start()
    for i in p_list:
        i.join()
    print("all subprocess done")
    while True:
        if queue.empty():
            break
        count += queue.get()
    return count
Exemplo n.º 15
0
    def run_machine(self, input_q: queue) -> Optional[int]:
        while self.code[self.i] != HALT:
            i = self.i
            opcode = self.code[i] % 100

            instr_count = INSTRUCTIONS_COUNT[opcode]
            first_arg, second_arg, third_arg = self.parse_arguments()

            if opcode == ADD:
                self.code[third_arg] = first_arg + second_arg
            elif opcode == MULTIPLY:
                self.code[third_arg] = first_arg * second_arg
            elif opcode == INPUT:
                # Halt if input is empty
                if input_q.empty():
                    return None
                else:
                    self.code[first_arg] = input_q.get()
            elif opcode == OUTPUT:
                self.i += instr_count
                return first_arg
            elif opcode == JUMP_IF_TRUE:
                if first_arg != 0:
                    self.i = second_arg
                    continue
            elif opcode == JUMP_IF_FALSE:
                if first_arg == 0:
                    self.i = second_arg
                    continue
            elif opcode == LESS_THAN:
                self.code[third_arg] = int(first_arg < second_arg)
            elif opcode == EQUALS:
                self.code[third_arg] = int(first_arg == second_arg)
            elif opcode == ADJUST_BASE:
                self.relative_base += first_arg
            else:
                print("Unexpected opcode", opcode)

            self.i += instr_count

        # If code is halt, return none.
        self.has_finished = True
        return None
Exemplo n.º 16
0
def nextFaceFromQueue(face: Tuple[int,int,int,int], frame: np.array, faceQueue: queue):
    """Gets the next face/obj, frame pair from the queue. If a new pair is not available
    it returns the face and frame that were passed in.
    
    Args:
        face (Tuple[int,int,int,int]): face/object (x,y,width,height)
        frame (np.array): frame that face/object was detected in
        faceQueue (queue): queue that stores face/object frame pairs (face/obj: Tuple[int,int,int,int], frame: np.array)
    
    Returns:
        (face/obj: Tuple[int,int,int,int], frame: np.array): the next face/obj frame pair
    """

    nextFace = face
    nextFrame = frame
    if not faceQueue.empty():
        try:
            nextFace, nextFrame = faceQueue.get_nowait()
        except queue.Empty:
            pass # We will just use the latest image we have
    return nextFace, nextFrame
Exemplo n.º 17
0
    def loop(max: int, min: int, threadid: str, mqueue: queue) -> None:
        """ Functie op een thread """
        global _STOP_THREADS

        index: int = (min | 1) - 2  # Zorg dat index oneven start en klaar is voor de lus

        while index < max and not _STOP_THREADS:  # Checken of er niet gestopt moet worden
            index += 2  # index updaten

            print("[{}] ".format(threadid), end='')

            if not is_prime_fast(index):  # Gemakkelijk te delen getallen verwerpen
                continue

            if n % index != 0:  # Niet deelbaar door n verwerpen
                continue

            if is_prime(index):  # Mogelijke candidaad voor oplossing
                print("Mogelijk gevonden resultaat {}".format(index))
                mqueue.put(index)  # uiteindelijk resultaat versturen naar main_thread

        print("[{}] Stoppen van thread op index= {}, max= {}, min= {}".format(threadid, index, max, min))
Exemplo n.º 18
0
    def __generate_generator_and_prime(id: int, bits_size: int,
                                       result_queue: queue):
        """
    Generates a safe prime number p and the generator of Zp, Zp = (Z, * mod p)
    Returns both numbers
    """
        safe_prime = Math.generate_safe_prime_number(
            bits_size)  # Gets a safe prime number p
        factor = (safe_prime - 1) // 2  # derivate a prime from safe prime.

        generator = max(2, secrets.randbelow(
            safe_prime -
            1))  # Choose a random integer in the range [2, safe_prime - 2]
        exponentation = Math.fast_exponentiation(generator, factor, safe_prime)

        while exponentation == 1:  # Some guy in stack overflow taugth this test to me.
            generator = max(2, secrets.randbelow(safe_prime - 1))
            exponentation = Math.fast_exponentiation(generator, factor,
                                                     safe_prime)

        result_queue.put(
            (generator,
             safe_prime))  # save in the queue the result of the operation.
Exemplo n.º 19
0
def readFrame(config, outputQ: queue):
    threshold = 0.5
    input_path = config.input_file
    max_frame = config.maxFrame
    cap = cv2.VideoCapture(input_path)
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = int(cap.get(cv2.CAP_PROP_FPS))
    total_cnt = cap.get(cv2.CAP_PROP_FRAME_COUNT)
    _inputCnt = 0
    pre_hist = None

    while True:
        ret, frame = cap.read()
        if not ret:
            break
        diff, hist = compare(frame, pre_hist)
        pre_hist = hist
        is_seg = False
        if diff > threshold:
            is_seg = True
        if max_frame > 0 and _inputCnt > max_frame:
            break
        qElem = {
            'index': _inputCnt,
            'decFrm': frame,
            'isSeg': is_seg,
            'total_cnt': total_cnt,
            'fps': fps
        }
        _inputCnt += 1
        # print(f'read frame:{_inputCnt}')
        outputQ.put(qElem)
    # end
    outputQ.put('EOF')
    logger.info('end readFrame')
    cap.release()
Exemplo n.º 20
0
    def trackFace(self, cameraStream: queue) -> None:
        """Tracks a face/object given a queue that contains objects and the frame the object was captured in.
        The queue object format is ((x,y,width,height), img: np.array)

        This function runs until stopAsyncFaceTracking() is called.
        
        Args:
            cameraStream (queue): queue with objects and frames
        """

        face = None
        frame = None
        # Get the first frame
        while face is None or frame is None:
            try:
                face, frame = cameraStream.get_nowait()
            except queue.Empty:
                pass
        self.tracking = True
        while(self.tracking):
            while(not faceCentered(face, frame) and self.tracking):
                direction = getObjectDirection(face, frame)
                if (direction[0] != Direction.NoDirection):
                    if (direction[0] == Direction.Left):
                        self.moveLeft()
                    elif (direction[0] == Direction.Right):
                        self.moveRight()
                if (direction[1] != Direction.NoDirection):
                    if (direction[1] == Direction.Up):
                        self.moveUp()
                    elif (direction[1] == Direction.Down):
                        self.moveDown()

                #Get the next image and face from queue
                face, frame = nextFaceFromQueue(face, frame, cameraStream)
            face, frame = nextFaceFromQueue(face, frame, cameraStream)
Exemplo n.º 21
0
        while not self.q.empty():
            crawler(self.q)
        print("Exiting ",self.pid)

def crawler(q):
    url=q.get(TimeoutError=2)
    try:
        r=requests.get(url,TimeoutError=20)
        print(q.qsize(),r.status_code,url)
    except Exception as e:
        print(q.qsize(),url,"Error: ",e)

if __name__=='__main__':
    ProcessNames=["Process-1","Process-2","Process-3","Process-4","Process-5","Process-6","Process-7"
                  ,"Process-8","Process-9","Process-10","Process-11"]
    workQueue=Queue(1000)

    #填充队列
    for url in link_list:
        workQueue.put(url)

    for i in range(0,11):
        p=MyProcess(workQueue)
        p.daemon=True
        p.start()
        p.join()

    end=time.time()
    print()

Exemplo n.º 22
0
 def process_job(q: queue):
     while True:
         next_job = q.get()
         print('Processing job:', next_job.description)
         q.task_done()
Exemplo n.º 23
0
class Commands:
    '''
    class to handle sending reports to device and parsing incoming reports.
    dynamically looks up/creates method for sending reports when none is
    found on the class.  this will let us override the default behavior
    to make things friendlier when appropriate.
    all received messages are queued up and require a call to 'process_received_reports'
    to trigger the user's callbacks, so as to avoid thread issues.
    '''
    def __init__(self, device):
        self.device = device
        self.callbacks = {}
        self.default_callbacks = set()
        self.queue = Queue()
        self.device.set_interrupt_report_callback(self._report_received)

    def _report_received(self, device, report_data):
        logging.info('%r', report_data)
        msg = REPORT.parse(report_data)
        logging.info('received msg: %r', msg)
        self.queue.put(msg)

    def process_received_reports(self, block=False, timeout=10):
        '''
        process all reports that have been received and call the
        relevant callbacks.
        by default this method returns immediately if no reports
        are on the queue, but can be made to block and wait for
        a report if needeed
        '''
        while block or not self.queue.empty():
            report = self.queue.get(block, timeout)
            self._process_report(report)
            # we always stop blocking after we've received
            # at least one report
            block = False

    def _process_report(self, report):
        callbacks = self.callbacks.get(report.id, self.default_callbacks)
        for callback in callbacks:
            callback(report)

    def get_received_reports(self):
        '''
        return a list of received reports (removes them from the queue)
        '''
        reports = []
        while not self.queue.empty():
            msg = self.queue.get()
            reports.append(msg)
        return reports

    def clear_received_reports(self):
        '''remove all received reports from the queue'''
        while not self.queue.empty():
            self.queue.get()

    def add_callback(self, report_id, report_callback):
        '''
        add a callback function that will be called when
        a report with the given id arrives. callback
        should take a single value that is the report
        that was received
        '''
        callbacks = self.callbacks.get(report_id, set())
        callbacks.add(report_callback)
        self.callbacks[report_id] = callbacks

    def remove_callback(self, report_id, report_callback):
        callbacks = self.callbacks.get(report_id, set())
        callbacks.discard(report_callback)  # remove if present

    def add_default_callback(self, report_callback):
        self.default_callbacks.add(report_callback)

    def remove_default_callback(self, report_callback):
        self.default_callbacks.discard(report_callback)

    def wait_for_report(self, report_id):
        '''
        blocks until we receive a report with the given id
        from the box and then returns it (may trigger other callbacks)
        '''
        # register a callback for the report we want
        reports = []

        def callback(report):
            reports.append(report)

        self.add_callback(report_id, callback)

        # process reports, until we get the one we want
        try:
            try:
                while len(reports) == 0:
                    self.process_received_reports(block=True, timeout=2)
            except Empty:
                return None
        finally:
            # then remove the callback, as we don't need it anymore
            self.remove_callback(report_id, callback)

        # return the report we received
        return reports[0]

    def send_wait_reply(self, command_id, report_id, *args):
        '''
        send a command with the given arguments and wait for the reply
        '''
        command_name = COMMAND.name_from_id(command_id).lower()
        getattr(self, command_name)(*args)  # send the command
        return self.wait_for_report(report_id)

    def send_wait_field(self, command_id, report_id, field_name, *args):
        '''
        send a command (with the args), wait for the report and return the field on the
        report
        '''
        return getattr(self.send_wait_reply(command_id, report_id, *args),
                       field_name)

    def __getattr__(self, name):
        '''return a function to send the named command to the device'''
        packing_function = getattr(COMMAND, name, None)
        if packing_function:
            return lambda *arg: self.device.set_report(packing_function(*arg))
        raise AttributeError("couldn't find: %s" % name)
Exemplo n.º 24
0
 def __init__(self, device):
     self.device = device
     self.callbacks = {}
     self.default_callbacks = set()
     self.queue = Queue()
     self.device.set_interrupt_report_callback(self._report_received)
Exemplo n.º 25
0
def email_log(logq:queue):
    loglist = []
    while not logq.empty():
        loglist.append(logq.get_nowait())
    logstrings = ["[{0}] {1}".format(le.levelname,le.message) for le in loglist]
    email_helper.send_email("[ponypackchangelog] ran at {0}".format(datetime.now()), "\n".join(logstrings))
Exemplo n.º 26
0
 def scale_service(self, scl: Scale, cluster, service, count: int,
                   q: queue):
     res = scl.scale_service(cluster, service, count)
     q.put(res)
Exemplo n.º 27
0
 def create_deployment(self, d: Deployment, cluster, service, tags: dict,
                       q: queue):
     res = d.create_deployment(cluster, service, tags)
     q.put(res)
Exemplo n.º 28
0
    def run_machine(self, input_q: queue) -> Optional[int]:
        while self.code[self.i] != HALT:
            i = self.i
            opcode = self.code[i] % 100
            first_mode = (self.code[i] // 100) % 10
            second_mode = (self.code[i] // 1000) % 10

            # Will throw an error if something goes wrong
            first_arg, second_arg, third_arg = -1, -1, -1

            instr_count = INSTRUCTIONS_COUNT[opcode]

            if opcode == ADD or opcode == MULTIPLY or opcode == LESS_THAN or opcode == EQUALS:
                first_arg = self.code[self.code[
                    i + 1]] if first_mode == 0 else self.code[i + 1]
                second_arg = self.code[self.code[
                    i + 2]] if second_mode == 0 else self.code[i + 2]
                third_arg = self.code[i + 3]
            elif opcode == JUMP_IF_TRUE or opcode == JUMP_IF_FALSE:
                first_arg = self.code[self.code[
                    i + 1]] if first_mode == 0 else self.code[i + 1]
                second_arg = self.code[self.code[
                    i + 2]] if second_mode == 0 else self.code[i + 2]
            elif opcode == INPUT:
                first_arg = self.code[i + 1]
            elif opcode == OUTPUT:
                first_arg = self.code[self.code[
                    i + 1]] if first_mode == 0 else self.code[i + 1]

            if opcode == ADD:
                self.code[third_arg] = first_arg + second_arg
            elif opcode == MULTIPLY:
                self.code[third_arg] = first_arg * second_arg
            elif opcode == INPUT:
                # Halt if input is empty
                if input_q.empty():
                    return None
                else:
                    self.code[first_arg] = input_q.get()
            elif opcode == OUTPUT:
                self.i += instr_count
                return first_arg
            elif opcode == JUMP_IF_TRUE:
                if first_arg != 0:
                    self.i = second_arg
                    continue
            elif opcode == JUMP_IF_FALSE:
                if first_arg == 0:
                    self.i = second_arg
                    continue
            elif opcode == LESS_THAN:
                self.code[third_arg] = int(first_arg < second_arg)
            elif opcode == EQUALS:
                self.code[third_arg] = int(first_arg == second_arg)
            else:
                print("Unexpected opcode", opcode)

            self.i += instr_count

        # If code is halt, return none.
        self.has_finished = True
        return None
Exemplo n.º 29
0
def call_tail_recursive_fib(n: int, q: queue):
    q.put(tail_recursive_fib(n))