将自定义数据传递给request_futures中的异常

奥科博科

我正在运行以下代码来发出异步“获取”请求。所述CustomSession类用于添加用于定时每个请求支持。

如果发生异常或请求运行正常,我希望能够访问test_id添加到futures列表的URL以及要请求的URL。换句话说,当请求运行或引发异常时,我想找到test_id与调用关联的对象session.get

from datetime import datetime
from requests_futures.sessions import FuturesSession

class CustomSession(FuturesSession):

    def __init__(self, *args, **kwargs):
        super(CustomSession, self).__init__(*args, **kwargs)
        self.timing = {}

    def request(self, method, url, *args, **kwargs):
        background_callback = kwargs.pop('background_callback', None)
        test_id = kwargs.pop('test_id', None)

        # start counting
        self.timing[test_id] = datetime.now()

        def time_it(sess, resp):
            # here if you want to time the server stuff only
            self.timing[test_id] = datetime.now() - self.timing[test_id]
            if background_callback:
                background_callback(sess, resp)
            # here if you want to include any time in the callback

        return super(CustomSession, self).request(method, url, *args,
                                                  background_callback=time_it,
                                                  **kwargs)


session = CustomSession()

futures = []
for url in ('http://httpbin.org/get?key=val',
            'http://httpasdfasfsadfasdfasdfbin.org/get?key2=val2'):

    futures.append(session.get(url, test_id=1))
for future in futures:
    try:
        r = future.result()
        print(r.status_code)
    except Exception as e:
        print(e)
1.618

我为将来对象的result()函数创建了一个装饰器:

def mark_exception(fn, id, url):
    def new_fn(*args, **kwargs):
        try:
            return fn(*args, **kwargs)
        except:
            raise Exception("test id %d with url %s threw exception" % (id, url))
    return new_fn

并将其应用于CustomSession.request()函数的末尾,替换原始的return语句:

future =  super(CustomSession, self).request(method, url, *args,
                                          background_callback=time_it,
                                          **kwargs)
future.result = mark_exception(future.result, test_id, url)
return future

输出:

200
test id 1 with url http://httpasdfasfsadfasdfasdfbin.org/get?key2=val2 threw exception

我希望这有帮助。

编辑:

如果您想获取每个将来的测试ID,可以通过以下两种方法进行:

futures = []
for url in ('http://httpbin.org/get?key=val',
            'http://httpasdfasfsadfasdfasdfbin.org/get?key2=val2'):
    tid = 1
    future = session.get(url, test_id=tid)
    # option 1: set test_id as an attrib of the future object
    future.test_id = tid
    # option 2: put test_id and future object in a tuple before appending to the list
    futures.append((tid, future))
for tid, future in futures:
    try:
        r = future.result()
        print("tracked test_id is %d" % tid) #option 2
        print("status for test_id %d is %d" % (future.test_id, r.status_code)) #option 1
    except Exception as e:
        print(e)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将自定义数据传递给React中的PrivateRoute组件

是否可以将自定义数据传递给条纹签出?

Haskell Servant将自定义数据传递给身份验证处理程序

如何将自定义数据传递给HandleChallengeAsync

如何将自定义数据传递给 Stripe webhook?

将自定义数据传递到vue-router中的$ router.push()

将自定义产品元数据传递到Woocommerce 4中的订单

将自定义产品元数据传递到Woocommerce 3中的订单

如何从ui-router中的视图将自定义数据传递到状态?

将自定义数据传递到Symfony / Twig中的表单主题

如何将自定义数据传递到Yii中的EDataTables

将自定义 bazel 规则的输出作为运行时数据传递给 *_binary 规则

AngularJS angular ui路由器-将自定义数据传递给控制器

将自定义的固定标头传递给 nginx 中的 auth_request

将视图数据传递给Kendo网格中的自定义编辑器

如何将自定义数据传递到Highcharts图单击事件

Laravel-将自定义数据传递到电子邮件视图

如何将自定义设置数据传递到src图像“ onClick”?

Spring RestTemplate Statuscodeexception将自定义消息传递给异常处理

有没有更好的方法将数据传递给 SwiftUI 中的自定义键盘视图?

使用 React Bootstrap 将数据传递给自定义元素

使用 WordPress 将表单数据传递给 API 自定义路由

通过 props 将数据传递给自定义的 mat-grid 组件

如何将 v-model 数据传递给自定义组件?

如何将数据传递给从布局调用的自定义视图助手-Zend

如何正确地将自定义数据属性道具传递给子组件?

将自定义Java数据模型传递给本机代码

将自定义用户字段传递给WooCommerce订单元数据

将自定义函数传递给pandas .agg()