【问题标题】:Using PyDev and Django in Eclipse and need to simulate a slow internet connection在 Eclipse 中使用 PyDev 和 Django,需要模拟慢速网络连接
【发布时间】:2013-10-17 17:56:26
【问题描述】:

我正在尝试实现一个 ajax 页面加载图标,但我当然无法在本地主机上对其进行测试,因为它会立即加载......

如何在 Ubuntu 中使用 Eclipse 平台模拟慢速互联网连接,这将允许我测试加载图标???

非常感谢

【问题讨论】:

    标签: django eclipse performance connection pydev


    【解决方案1】:

    这对中间件很有用。 Here 是一个中间件示例,可以将每个请求延迟一定量,可在您的设置中进行配置:

    """
    This module provides very simple Django middleware that sleeps on every request.
    
    This is useful when you want to simulate slow response times (as might be
    encountered, say, on a cell network).
    
    To use, add this middleware, and add a value for SLEEP_TIME to your settings.
    
    Possible future feature: Look for an X-Django-Sleep header on each request,
    to let the client specify per-request sleep time.
    """
    
    import time
    
    import django.conf
    import django.core.exceptions
    
    
    class SleepMiddleware(object):
    
        def __init__(self):
            self.sleep_time = getattr(django.conf.settings, "SLEEP_TIME", 0)
            if not isinstance(self.sleep_time, (int, float)) or self.sleep_time <= 0:
                raise django.core.exceptions.MiddlewareNotUsed
    
        def process_request(self, request):
            time.sleep(self.sleep_time)
    

    您可以从这里变得更有趣,例如,加入随机性,这在测试复杂的 ajax 交互时很有用。

    【讨论】:

    • 这看起来很棒,非常感谢!我现在无法测试,但我会尽快测试并报告!
    【解决方案2】:

    在您的视图中输入time.sleep(5),然后返回睡眠状态五秒钟。

    【讨论】:

    • 好的,最后我用它作为临时和简单的解决方案来做我需要的事情。但是,使用 time.sleep 不起作用 - 我做到了:from time import sleep,然后是 sleep(5)。但是非常感谢你让我完成了 90% 的路!!
    【解决方案3】:

    对于 200 毫秒的延迟,我只需使用以下命令:

    sudo tc qdisc add dev enp2s0 root netem delay 200ms
    

    add 更改为del 以返回原始状态。

    您可以通过在延迟的机器上执行 ping 来监控事情的变化。

    我是从here那里拿到的。

    【讨论】:

      猜你喜欢
      • 2012-04-09
      • 2010-11-08
      • 2011-02-27
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多