我有一个非常简单的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) 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初始化后稍后调用但是我更喜欢用引用来做这个,这意味着我需要在它中初始化它构造函数,因此问题.
#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,我希望编译器能够捕获它.我不希望它在运行时爆炸.
移动应用程序如何与服务器通信?这种通信是通过HTTP进行还是有其他方法(例如,我猜移动应用程序可以打开套接字)?这与设备不同吗?有一种最常用的方法吗?如果通过HTTP进行通信,URL如何?如果通过HTTP进行通信,有没有办法根据HTTP请求中的"用户代理"识别应用程序?
我使用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),如果它是阻塞.但我可以杀死旋转的线程(进行计算).如果有,怎么样?如果没有,为什么不呢?
这会因额外的空格而导致问题:
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++ ×4
boost ×1
constructor ×1
crtp ×1
gevent ×1
mobile ×1
polymorphism ×1
python ×1
python-2.7 ×1