Пример #1
0
def clientMain(port, resultsName, pathToTasks, pathToPreviouslyCompletedTasks = None):
    '''Starts the server. 
    
    Args:
        - port -- port on which the server should listen
        - resultsName -- name to which to append the currentTime in order to create a unique string
        - pathToTasks -- path to the tasks file
        - pathToPreviouslyCompletedTasks -- path to a file containing tasks that have already been completed and should not be executed again.
    '''

    #-------------initialization of values
    timeName = resultsName+datetime.datetime.now().strftime('%d%b%Y%H%M%S.%f') # a unique id created from the time when the script starts 
    messageQueue = multiprocessing.Queue() # the queue where all log messages are written to.
    taskQueue = multiprocessing.Queue() # the queue where all tasks that have been loaded into memory and have not been completed are kept
    logControlValue = multiprocessing.Value('i',int(True)) #A boolean indicating the the logging process should continue to run. There is no type for bool, so we use int
    numberOfConnectionsValue = multiprocessing.Value('i',0) #An integer counting the number of clients that are currently connected to the server
    numberOfConnectionsLock = multiprocessing.Lock() #A Lock that should be acquired when modifying the numberOfConnections value
    IOLock = multiprocessing.Lock() #A lock that should be acquired when performing writing of results to memory
    signal.signal(signal.SIGINT, signal_handler) #register our signal_handler so that we can detect ctrl-C signals
    resultsDirectory, logsDirectory = UtilityFunctions.initializeServerFolderStructure(timeName) #directories where results and logs should be stored respectively

    if not ServerSideTaskHandler.initializeTaskQueue(taskQueue, pathToTasks,resultsDirectory,pathToPreviouslyCompletedTasks):
        sys.exit(1)# initialize Task Queue returns false if initialization and loading of tasks failed
    #-------------done initializing
    
    #-------------start logging module
    loggingProcess = multiprocessing.Process(target = LoggingModule.log, args=(logControlValue,messageQueue,logsDirectory))
    loggingProcess.start()

    #Start listening for clients:    
    #ListenForClients(port,messageQueue,taskHandler) #DEBUG
    listeningProcess = multiprocessing.Process(target = ListenForClients, args=(port, messageQueue, taskQueue, IOLock, numberOfConnectionsValue, numberOfConnectionsLock,resultsDirectory))
    listeningProcess.start()
    
    
    
    #idle while there are clients connected and tasks still incomplete
    now = datetime.datetime.now()
    logPeriod = datetime.timedelta(minutes=10)
    messageQueue.put(UtilityFunctions.createLogEntry('inf','Periodic log entry, there are currently:\t ' 
                             + str(taskQueue.qsize()) + ' tasks yet to complete'))
    while numberOfConnectionsValue.value>0 or taskQueue.qsize() > 0:
        if datetime.datetime.now() > now+logPeriod:
            messageQueue.put(UtilityFunctions.createLogEntry('inf','Periodic log entry, there are currently:\t ' 
                             + str(taskQueue.qsize()) + ' tasks yet to complete'))
            now = datetime.datetime.now()
        time.sleep(1)
    
    messageQueue.put(UtilityFunctions.createLogEntry('inf','Completed all tasks, exiting'))
    listeningProcess.terminate()
    logControlValue.value = int(False)
    loggingProcess.join()
    listeningProcess.join()
    sys.exit(0)
Пример #2
0
def clientMain(port,
               resultsName,
               pathToTasks,
               pathToPreviouslyCompletedTasks=None):
    '''Starts the server. 
    
    Args:
        - port -- port on which the server should listen
        - resultsName -- name to which to append the currentTime in order to create a unique string
        - pathToTasks -- path to the tasks file
        - pathToPreviouslyCompletedTasks -- path to a file containing tasks that have already been completed and should not be executed again.
    '''

    #-------------initialization of values
    timeName = resultsName + datetime.datetime.now().strftime(
        '%d%b%Y%H%M%S.%f'
    )  # a unique id created from the time when the script starts
    messageQueue = multiprocessing.Queue(
    )  # the queue where all log messages are written to.
    taskQueue = multiprocessing.Queue(
    )  # the queue where all tasks that have been loaded into memory and have not been completed are kept
    logControlValue = multiprocessing.Value(
        'i', int(True)
    )  #A boolean indicating the the logging process should continue to run. There is no type for bool, so we use int
    numberOfConnectionsValue = multiprocessing.Value(
        'i', 0
    )  #An integer counting the number of clients that are currently connected to the server
    numberOfConnectionsLock = multiprocessing.Lock(
    )  #A Lock that should be acquired when modifying the numberOfConnections value
    IOLock = multiprocessing.Lock(
    )  #A lock that should be acquired when performing writing of results to memory
    signal.signal(
        signal.SIGINT, signal_handler
    )  #register our signal_handler so that we can detect ctrl-C signals
    resultsDirectory, logsDirectory = UtilityFunctions.initializeServerFolderStructure(
        timeName
    )  #directories where results and logs should be stored respectively

    if not ServerSideTaskHandler.initializeTaskQueue(
            taskQueue, pathToTasks, resultsDirectory,
            pathToPreviouslyCompletedTasks):
        sys.exit(
            1
        )  # initialize Task Queue returns false if initialization and loading of tasks failed
    #-------------done initializing

    #-------------start logging module
    loggingProcess = multiprocessing.Process(target=LoggingModule.log,
                                             args=(logControlValue,
                                                   messageQueue,
                                                   logsDirectory))
    loggingProcess.start()

    #Start listening for clients:
    #ListenForClients(port,messageQueue,taskHandler) #DEBUG
    listeningProcess = multiprocessing.Process(
        target=ListenForClients,
        args=(port, messageQueue, taskQueue, IOLock, numberOfConnectionsValue,
              numberOfConnectionsLock, resultsDirectory))
    listeningProcess.start()

    #idle while there are clients connected and tasks still incomplete
    now = datetime.datetime.now()
    logPeriod = datetime.timedelta(minutes=10)
    messageQueue.put(
        UtilityFunctions.createLogEntry(
            'inf', 'Periodic log entry, there are currently:\t ' +
            str(taskQueue.qsize()) + ' tasks yet to complete'))
    while numberOfConnectionsValue.value > 0 or taskQueue.qsize() > 0:
        if datetime.datetime.now() > now + logPeriod:
            messageQueue.put(
                UtilityFunctions.createLogEntry(
                    'inf', 'Periodic log entry, there are currently:\t ' +
                    str(taskQueue.qsize()) + ' tasks yet to complete'))
            now = datetime.datetime.now()
        time.sleep(1)

    messageQueue.put(
        UtilityFunctions.createLogEntry('inf', 'Completed all tasks, exiting'))
    listeningProcess.terminate()
    logControlValue.value = int(False)
    loggingProcess.join()
    listeningProcess.join()
    sys.exit(0)