Example #1
0
def main(workers=10):
    """
    Executes main function of mini-framework's Control thread.
    :param workers: Integer detailing number of worker FIFO threads to employ
    """
    start_logging()
    log_info("New multiprocessing session with {} workers".format(workers))

    # Input JoinableQueue and Output Queue
    inq = JoinableQueue(maxsize=int(workers * 1.5))
    outq = Queue(maxsize=int(workers * 1.5))

    ot = OutThread(workers, outq)
    ot.start()

    for _ in range(workers):
        w = WorkerThread(inq, outq)
        w.start()

    # Create a sequence of a 1000 random alphabetic characters
    random_chars = (ascii_letters[randint(0, 51)] for _ in range(1000))

    # Keep input queue loaded for as long as possible
    # Feed the process pool with work units
    for work in enumerate(random_chars):
        inq.put(work)

    # Fill the input queue with Nones to shut the worker threads down
    # which terminates the process pool
    for _ in range(workers):
        inq.put(None)

    inq.join()
    print("Control process terminating")
Example #2
0
def main(workers=10):
    """
    Executes main function of mini-framework's Control thread.
    :param workers: Integer detailing number of worker FIFO threads to employ
    """
    start_logging()
    log_info("New multiprocessing session with {} workers".format(workers))
    
    # Input JoinableQueue and Output Queue
    inq = JoinableQueue(maxsize=int(workers*1.5))
    outq = Queue(maxsize=int(workers*1.5))
    
    ot = OutThread(workers, outq)
    ot.start()
    
    for _ in range(workers):
        w = WorkerThread(inq, outq)
        w.start()
    
    # Create a sequence of a 1000 random alphabetic characters
    random_chars = (ascii_letters[randint(0, 51)] for _ in range(1000))
    
    # Keep input queue loaded for as long as possible
    # Feed the process pool with work units
    for work in enumerate(random_chars):
        inq.put(work)
    
    # Fill the input queue with Nones to shut the worker threads down
    # which terminates the process pool
    for _ in range(workers):
        inq.put(None)
        
    inq.join()
    print("Control process terminating")
 def __init__(self, f):
     """
     Stores a reference to a proxied function.
     :param f: Function namespace to be proxied
     """
     self.func = f
     start_logging()
     log_info("New Composable proxied function: {0}".format(f.__name__))
 def __init__(self, f):
     """
     Stores a reference to a proxied function.
     :param f: Function namespace to be proxied
     """
     self.func = f
     start_logging()
     log_info("New Composable proxied function: {0}".format(f.__name__))
Example #5
0
def main():
    start_logging()

    for chunk_size in 1000, 10000, 100000, 1000000:
        w = Timer("file_writer({})".format(chunk_size),
                  "from __main__ import file_writer")
        mm = Timer("mmap_file_writer({})".format(chunk_size),
                   "from __main__ import mmap_file_writer")

        wt = "file_writer took {} w/chunks of {}".format(
            w.timeit(number=1), chunk_size)
        mt = "mmap_file_writer took {} w/chunks of {}".format(
            mm.timeit(number=1), chunk_size)

        print(wt)
        print(mt)
        log_info(wt)
        log_info(mt)
def main():
    start_logging()
    
    for chunk_size in 1000, 10000, 100000, 1000000:
        w = Timer("file_writer({})".format(chunk_size),
                          "from __main__ import file_writer")
        mm = Timer("mmap_file_writer({})".format(chunk_size),
                           "from __main__ import mmap_file_writer")
        
        wt = "file_writer took {} w/chunks of {}".format(w.timeit(number=1),
                                                         chunk_size)
        mt = "mmap_file_writer took {} w/chunks of {}".format(mm.timeit(number=1),
                                                              chunk_size)
        
        print(wt)
        print(mt)
        log_info(wt)
        log_info(mt)
Example #7
0
    def run(self):
        " Extract items from the output queue and print until all done. "

        while self.workers:
            p = self.queue.get()

            if p is None:
                self.workers -= 1

            else:
                # This is a real output packet
                self.output.append(p)

        start_logging()
        output_txt = "Final length of random string: {}".format(len(self.output))
        print(output_txt)
        log_info(output_txt)

        print("Output process terminating")
        sys.stdout.flush()
Example #8
0
    def run(self):
        " Extract items from the output queue and print until all done. "

        while self.workers:
            p = self.queue.get()

            if p is None:
                self.workers -= 1

            else:
                # This is a real output packet
                self.output.append(p)

        start_logging()
        output_txt = "Final length of random string: {}".format(
            len(self.output))
        print(output_txt)
        log_info(output_txt)

        print("Output process terminating")
        sys.stdout.flush()
"""
Created on Sep 25, 2015

@author: cmontesr

Unittest module for the Control module of the Python4_Homework11 package
"""
import unittest
import control
import sys
import re
from io import StringIO
from project_log import start_logging, log_info
from timeit import Timer

start_logging()


class TestThreadingRandomSTR(unittest.TestCase):
    def test_console_output_and_time(self):
        " Tests the resulting console output from the framework's output. "

        regex_workers = re.compile("(Worker Thread-[\d]+ done[\n]){10}")

        saved_output = sys.stdout

        try:
            out = StringIO()
            sys.stdout = out

            control.main()
# Lesson 03: "Delegation and Composition" Homework
#
__author__ = "cmontesr"
'''
Created on Sep 7, 2015

tree_extended.py: Expands the logic of the Tree class found in
                  Lesson 03, so it takes extra attributes to be
                  stored and a find() method.
'''

from project_log import start_logging
from logging import info as log_info
from logging import error as log_error

start_logging()

class Tree:
    """
    Displays use of delegation and recursiveness.
    """
    def __init__(self, key, data=None):
        """
        Create a new Tree object with empty L & R subtrees.
        :param key: Object to be used as the identifier/key of this node
        :param data: Data to be associated with this node
        """
        self.key = key
        self._data = data
        self.left = self.right = None