-3
\$\begingroup\$
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

\$\endgroup\$
1
  • 2
    \$\begingroup\$ 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.) \$\endgroup\$ Commented Apr 25, 2015 at 7:40

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.