Treatment plant model using two concurrent and sequential execution

2010-04-07  来源:本站原创  分类:Python  人气:161 

Often used in the program concurrent with the sequential case, each time requiring concurrent time deciding on the allocation of the thread, then, would appear the code is a mess, so here I use a factory class to create different execution ExecutorFactory Category: ConcurExecutor and SequenExecutor.

Look at the code again:
ExecutorFactory.py:

from ConcurExecutor import ConcurExecutor
from SequenExecutor import SequenExecutor

class ExecutorFactory:
    def __init__(self):
        pass

    def createExecutor(self, type):
        if "concurrence" == type:
            executor = ConcurExecutor()
        elif "sequence" == type:
            executor = SequenExecutor()

        return executor

ConcurExecutor.py:

import threading

class JobThread(threading.Thread):
    def __init__(self, func, args):
        threading.Thread.__init__(self)
        self.func = func
        self.args = args
        self.result = False
    def run(self):
        self.result = apply(self.func, self.args)
    def getResult(self):
        return self.result

class ConcurExecutor:
    def __init__(self):
        self.jobList = []
    def addJob(self, jobFunc, jobArgs, env):
        jobThread = JobThread(jobFunc, (jobArgs, env))
        self.jobList.append(jobThread)
    def execJobs(self):
        for jobThread in self.jobList:
            jobThread.start()
        for jobThread in self.jobList:
            jobThread.join()
    def isAllSucc(self):
        for jobThread in self.jobList:
            if False == jobThread.getResult():
                return False
        return True

SequenExecutor.py:

class SequenExecutor:
    def __init__(self):
        self.jobList = []
        self.resultList = []
    def addJob(self, jobFunc, jobArgs, env):
        job = (jobFunc, jobArgs, env)
        self.jobList.append(job)

    def execJobs(self):
        for job in self.jobList:
            result = job[0](job[1], job[2])
            self.resultList.append(result)

    def isAllSucc(self):
        if all(result for result in self.resultList):
            return True
        return False

When we use can be written:

executorFactory = ExecutorFactory()

executor = executorFactory.createExecutor('sequence') # Of course there can also be changed to concurrency
    for elm_scene in elm_branch.getchildren():
        executor.addJob(SceneAction, (elm_scene, brchLogger), envClone(env))
    executor.execJobs()
    result = executor.isAllSucc()

Thus, when our project, the number of concurrent or sequential treatment to local needs, when not very confusing.

相关文章
  • Treatment plant model using two concurrent and sequential execution 2010-04-07

    Often used in the program concurrent with the sequential case, each time requiring concurrent time deciding on the allocation of the thread, then, would appear the code is a mess, so here I use a factory class to create different execution ExecutorFa

  • java memory model (JMM) 2009-03-17

    Memory model (memory model) Memory model describes the procedures for each variable (instance fields, static fields and array elements) the relationship between the computer systems as well as in the real variables will be stored in memory and remove

  • java memory model (transfer) 2010-04-07

    Memory model (memory model) Memory model describes the process variables (instance fields, static fields and array elements) the relationship between and the variables in the actual computer system will store the memory and remove the variable from m

  • JavaScript object model - implementation of model 2009-05-18

    http://www.cnblogs.com/RicCC/archive/2008/02/15/JavaScript-Object-Model-Execution-Model.html Cow too, and it is the browser developers. . . . Data Type <br /> The basic data types are basic data types of the JS implementation language of the bottom.

  • Understanding of the factory model 2010-04-01

    A simple understanding of plant model Simple Factory Pattern First, if a customer to use a cell phone, the general practice is to create a mobile client, then use of them: At this time, customers need to know how to create a mobile phone, customers,

  • I understand the "factory model" 2010-04-18

    Factory pattern, I prefer to call it a "parent mode" or "family model." Suppose we are children, we need food clothes and toys. If I sell food to the boss, the boss of selling clothes, toys sold by the owner, I will owe them something.

  • PHP single-piece model in several ways 2010-05-09

    Single model we used in developing a design model Often , Li Yong PHP5 object-oriented Texing, we can easily to build single mode of application, the following is the singleton pattern in PHP in the Jizhong achieve Fangfa : class Stat{ static $instan

  • How do industrial wastewater treatment 2010-07-24

    Now, China's coke, pesticides, fertilizers, chemicals, rare earth smelting, iron oxide red pigments such as emissions of industrial enterprises and the high concentration of ammonia nitrogen high concentration of waste water is more common, because t

  • Summary SSH interview questions 2010-12-23

    Summary SSH interview questions MVC 1. Talk about your understanding of mvc MVC is the Model-View-Controler for short. The Model - View - Controller. MVC is a design pattern that the mandatory application of the input, processing and output separatel

  • 2000个软件开发领域的高频特殊词及精选例句(七) 2015-03-20

    superword是一个Java实现的英文单词分析软件,主要研究英语单词音近形似转化规律.前缀后缀规律.词之间的相似性规律等等. 1701.单词 jpql 的匹配文本: You will learn how to implement the various database operations using JPA's EntityManager interface and JPQL (which is similar to HQL) Pro Spring 3 In another situat

  • Introduction Compact design - the construction of error-free software 2009-08-26

    Thanks to the translator's hard to pay, but also thanks to the wisdom of sharing original author. Reprinted from Mr: http://softwarepractitioner.org/translations/meyer/contract.shtml ================================================== ================

  • Printing of pre-treatment water 2010-02-16

    Keyword: Printing and dyeing textile pre-treatment water current general trend of development is to finish, deep processing, high-end, diversified, when Xinhua, decorative, functional and so forth, and to increase the textiles of "value added" t

  • Java design patterns factory pattern 2010-05-30

    Reprinted: http://blog.csai.cn/user1/25929/archives/2009/35622.html 1, primer said that ten years ago, one upstart, his family has three cars (Benz (Mercedes), Bmw (BMW), Audi (Audi) It seems that people more patriotic, no Japanese cars), enough to h

  • Cluster mass distribution index data 2010-07-19

    Stated, the article from the development team soso Search index data storage is a very important aspect, in the case of small amounts of data, such as the common medium and small sites, there is generally a document number in the tens of millions of

  • 8) Linux Programming Introduction - network programming (under) 2010-09-26

    Network programming (7) 7. TCP / IP protocol You may have heard of TCP / IP protocol, then in the end you know what is TCP, what is the IP you? In this chapter which, as we Up to learn this now the most widely used network protocol. 7.1 Layered Netwo

  • One of IO performance: the performance of several indicators to measure 2010-10-18

    Preface Some basic concepts The size of a single IO (IO Chunk Size) IOPS (IO per Second) Transmission speed (Transfer Rate) / Throughput (Throughput) IO response time (IO Response Time) Further reading Note: This series of articles that are involved

  • Annotation Processing Tool (APT) 2010-11-21

    Reference: Thinking in Java annotations chapter Annotation Processing Tool Official Description In Java, there is a command line tool apt (also includes the javac command function, which is compiled function), its role is to compile the source files

  • Js cross-domain problem solving 2010-12-03

    How to solve cross-domain issues js Js web developers to cross-domain problem is a problem in one of the most commonly encountered. Js so-called cross-domain issue, refers to the page under a field visit by js data under a different domain objects, f

  • [Hosted services] to prevent DDOS attack servers with effective measures CC 2011-05-06

    Principles of Attack CC is used to attack the main page. We have the experience, that is, visit the forum, if this forum is bigger, more people access to open pages slowly on, right? ! In general, the more people visit the forum more pages, database,

  • 2000个软件开发领域的高频特殊词及精选例句(四) 2015-03-20

    superword是一个Java实现的英文单词分析软件,主要研究英语单词音近形似转化规律.前缀后缀规律.词之间的相似性规律等等. 801.单词 spellchecker 的匹配文本: Setting this property to values higher than the size parameter can result in a more accurate document frequency (this is because of the fact that terms are he