小编Nik*_*hil的帖子

使用Gevent Pool的Python脚本消耗大量内存,锁定

我有一个非常简单的Python脚本,使用gevent.pool下载URL(见下文).该脚本可以运行几天,然后锁定.我注意到那时的内存使用率非常高.我错误地使用了gevent吗?

import sys

from gevent import monkey
monkey.patch_all()
import urllib2

from gevent.pool import Pool

inputFile = open(sys.argv[1], 'r')
urls = []
counter = 0
for line in inputFile:
    counter += 1
    urls.append(line.strip())
inputFile.close()

outputDirectory = sys.argv[2]

def fetch(url):
    try:
        body = urllib2.urlopen("http://" + url, None, 5).read()
        if len(body) > 0:
            outputFile = open(outputDirectory + "/" + url, 'w')
            outputFile.write(body)
            outputFile.close()
            print "Success", url
    except:
        pass

pool = Pool(int(sys.argv[3]))
pool.map(fetch, urls)
Run Code Online (Sandbox Code Playgroud)

python gevent python-2.7

5
推荐指数
1
解决办法
1100
查看次数

可以在以下示例中的构造函数中传递*this

Class A
{
  A(B& b) : mb(b) 
   {
     // I will not access anything from B here
    }

  B& mb;
};


Class B
{
B(): a(*this)
{}

A a;
}
Run Code Online (Sandbox Code Playgroud)

我可能遇到过这样的情况,包含的对象需要使用容器功能.在包含的对象中引用容器对象似乎是执行此操作的最佳方法.当然,我可以用一个指针来做这个,这样我可以有一个setter setB(B* b) {mb = b;},我可以在我确定B初始化后稍后调用但是我更喜欢用引用来做这个,这意味着我需要在它中初始化它构造函数,因此问题.

c++ constructor

3
推荐指数
1
解决办法
143
查看次数

这是静态多态的CRTP用法,但没有实现派生函数.在gcc和visual studio中编译.为什么?

#include <iostream>

template <class Derived>
class Base
{
public:
  void method1()
  {
    static_cast<Derived*>(this)->method1();
  }

  void method2()
  {
    static_cast<Derived*>(this)->method2();
  }
};

class Derived1: public Base<Derived1>
{
public:
  void method1()
  {
    std::cout << "Method 1 of Derived1 executed.\n";
  }
};

int main(int argc, char *argv[])
{
  Derived1 d1;
  d1.method1();
  d1.method2();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

跟进问题:如何使这种类型安全?也就是说,如果有人忘记实现method2,我希望编译器能够捕获它.我不希望它在运行时爆炸.

c++ polymorphism crtp

3
推荐指数
1
解决办法
460
查看次数

移动应用程序如何与服务器通信?

移动应用程序如何与服务器通信?这种通信是通过HTTP进行还是有其他方法(例如,我猜移动应用程序可以打开套接字)?这与设备不同吗?有一种最常用的方法吗?如果通过HTTP进行通信,URL如何?如果通过HTTP进行通信,有没有办法根据HTTP请求中的"用户代理"识别应用程序?

mobile

3
推荐指数
2
解决办法
8026
查看次数

是否有可能杀死旋转的线程?

我使用ZThreads来说明问题,但我的问题适用于C++中的PThreads,Boost Threads和其他类似的线程库.

class MyClass: public Runnable
{
 public:
  void run()
   {
      while(1)
      {

      }
   }
}
Run Code Online (Sandbox Code Playgroud)

我现在推出如下:

MyClass *myClass = new MyClass();
Thread t1(myClass);
Run Code Online (Sandbox Code Playgroud)

现在是否可以杀死(必要时暴力)这个线程?我可以肯定这样做,而不是我有一个无限循环Thread::Sleep(100000),如果它是阻塞.但我可以杀死旋转的线程(进行计算).如果有,怎么样?如果没有,为什么不呢?

c++ multithreading

3
推荐指数
1
解决办法
1212
查看次数

由于boost :: lexical cast中额外的空格引起的问题

这会因额外的空格而导致问题:

std::string t("3.14 ");
double d = boost::lexical_cast<double> (t); 
Run Code Online (Sandbox Code Playgroud)

所以,我写了这个

template<typename T> 
T string_convert(const std::string& given)
{
  T output;
  std::stringstream(given) >> output;
  return output;
}

double d = string_convert<double> (t); 
Run Code Online (Sandbox Code Playgroud)

这会有什么问题?有没有更好的办法?更喜欢使用词法演员

c++ boost

1
推荐指数
1
解决办法
2172
查看次数