Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
parse_client = None
store_client = None
def init_client():
    global parse_client
    global  store_client

    // ... some codes

    factory = ClientFactory(config)
    parse_client = factory.getJdParseClient()
    store_client = factory.getJdStoreClient()


def has_jd(jd_url):

    jd_id = parse_client.parseJdId(jd_source, jd_url)
    return store_client.listJd("", [jd_id])

def caculate(concurrency, jd_list):
    with futures.ProcessPoolExecutor(max_workers=concurrency) as executor:
        for jd_item in jd_list:
            job = executor.submit(check_url, jd_item)


def check_url(jd_item):
    jd_date, jd_source, url = jd_item
    return (jd_date, jd_source, url, has_jd(url))

def main():
    init_client()
    concurrency = 15

    jd_count, jd_item_list = parse_files()
    caculate(concurrency, jd_item_list)


if __name__ == '__main__':
    main()

I want to know if I have better choice instead of the global variable parse_client and store_client.

The problem is if I change these two variable from global to local variable, I need to init them every time, but these two variable is generate by the ClientFactory which need remote producedure call(rpc), so the cost is too much

share|improve this question

closed as unclear what you're asking by 200_success Apr 25 at 7:40

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
This question looks like it is somewhere between unclear and generic/hypothetical. You can make it clear and on-topic by describing what the code is about and retitling the question to reflect the purpose of the code, not how you want it reworked. (See How to Ask.) –  200_success Apr 25 at 7:40

1 Answer 1

up vote 1 down vote accepted

So you have a bunch of methods that need to work with the same data. Instead of using global variables for the shared data, a simple alternative can be wrapping these methods in a class. You can initialize the shared data in the constructor, and the methods will be able to access them via self.parse_client and self.store_client. The code in the current init_client could be in the constructor, and you can delete this function.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.