如何获取python函数使用的线程?

时间:2019-04-01 09:20:07

标签: python multithreading python-multithreading

我正在尝试在this doc page中找到用于python的多线程示例代码

实际代码如下:

import concurrent.futures
import urllib.request

URLS = ['http://www.foxnews.com/',
        'http://www.cnn.com/',
        'http://europe.wsj.com/',
        'http://www.bbc.co.uk/',
        'http://some-made-up-domain.com/']

# Retrieve a single page and report the URL and contents
def load_url(url, timeout):
    with urllib.request.urlopen(url, timeout=timeout) as conn:
        return conn.read()

# We can use a with statement to ensure threads are cleaned up promptly
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    # Start the load operations and mark each future with its URL
    future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}
    for future in concurrent.futures.as_completed(future_to_url):
        url = future_to_url[future]
        try:
            data = future.result()
        except Exception as exc:
            print('%r generated an exception: %s' % (url, exc))
        else:
            print('%r page is %d bytes' % (url, len(data)))

我希望load_url函数打印出当前正在使用的线程的ID,以便我可以监视多线程是否真正起作用。当然,如果您有更好的方法实现相同的目标,请告诉我。

谢谢

修改

我想我只是碰上了答案,这似乎可行

# Retrieve a single page and report the URL and contents
import threading
def load_url(url, timeout):
    print('Using thread {}, looking for url {}'.format(threading.get_ident(), url))
    with urllib.request.urlopen(url, timeout=timeout) as conn:
        return conn.read()

但是,欢迎您提供有关首选方法的任何反馈。

0 个答案:

没有答案