鉴于在发送HTTP请求时已建立TCP连接,因此隐式知道IP地址和端口 - TCP连接是IP +端口.那么,为什么我们需要Host标题?这是否只有在多个主机映射到TCP连接中隐含的IP地址的情况下才需要?
import signal
import sys
import time
def sigint_handler(signal, frame):
print "signal"
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
while 1:
try:
print "text"
time.sleep(2)
except KeyboardInterrupt:
print "keybi"
exit(0)
except:
print "except"
continue
Run Code Online (Sandbox Code Playgroud)
当我按下时,Ctrl-C我看到"信号"和"除外",程序没有退出.
为什么程序没有退出,而它显然达到了sys.exit(0)?
为什么程序流不到达该KeyboardInterrupt部分?
在不退出的情况下,在不同的地方分别Ctrl-C处理和处理每个except:案例的简洁方法是什么?
旨在运行所有内容的主文件现在是十几个变量(默认情况下在顶层控制流中是全局变量),几个结构和一个中型主循环.从其他语言我了解到全局变量是邪恶的.我倾向于将它全部放入同一个文件中的一个类中,并从全局控制流中调用一个方法,如下所示:
def MyClass:
def __init__(self):
self.value1 = 1
....
if __name__ == "__main__":
#inspect sys.argv here
MyClass().main_proc()
Run Code Online (Sandbox Code Playgroud)
你认为这是一个设计加吗?有没有pythonic方式来做到这一点?
我想记录原始字节。但是,如果我将FileHandler中的文件模式从“ w”更改为“ wb”,则记录器将失败,并出现错误,无论我传递给它的是哪个数据:字符串还是字节。
logging.getLogger("clientIn").error(b"bacd")
Traceback (most recent call last):
File "/usr/lib/python3.4/logging/__init__.py", line 980, in emit
stream.write(msg)
TypeError: 'str' does not support the buffer interface
Call stack:
File "<string>", line 1, in <module>
File "/usr/lib/python3.4/multiprocessing/spawn.py", line 106, in spawn_main
exitcode = _main(fd)
File "/usr/lib/python3.4/multiprocessing/spawn.py", line 119, in _main
return self._bootstrap()
File "/usr/lib/python3.4/multiprocessing/process.py", line 254, in _bootstrap
self.run()
File "/usr/lib/python3.4/multiprocessing/process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "/home/serj/work/proxy_mult/proxy/connection_worker_process.py", line 70, in __call__
self._do_work(ipc_socket)
File "/home/serj/work/proxy_mult/proxy/connection_worker_process.py", line 76, in _do_work
logging.getLogger("clientIn").error("bacd")
Message: 'bacd' …Run Code Online (Sandbox Code Playgroud) 当我们执行以下操作时会发生什么:1)
int i = -1; // 32 bit
void *p;
p = reinterpret_cast<void*>(i)
Run Code Online (Sandbox Code Playgroud)
在64位架构上,sizeof(void*)== 8
2)
long long i; // 64 bit
void *p = (unsigned int)(-1);
i = retinterpret_cast<long long>(p)
Run Code Online (Sandbox Code Playgroud)
在32位架构sizeof(void*)= 4
我通常理解结果会是什么,但我希望有人能够根据C++标准来描述机制,以便更好地理解.
在第二种情况下,行为类似于"整数提升"(4.5)(我将为-1)中描述的情况"int - unsigned long",所以我们通常说指针转换为有符号整数?
3)
int i = ...;
unsigned long long L = ...;
i = L;
Run Code Online (Sandbox Code Playgroud)
这里有什么规则?
相当常见的面试问题:如何在C++中声明一个由不同线程使用的变量?(或类似的东西).我以为'volatile int x;' 是答案,但我从一些评论中看到这是不正确的.我对两者都感兴趣:
在测试中遇到以下任务:
#include <iostream> using namespace std;
template<typename T> void adl(T) { cout << "T"; }
struct S { };
template<typename T> void call_adl(T t) { adl(S()); adl(t); }
void adl(S) { cout << "S"; }
int main () { call_adl(S()); }
Run Code Online (Sandbox Code Playgroud)
问题是将调用哪些函数.还有一种解释是,在模板定义时解析不依赖于模板参数的函数的名称,而在知道模板参数时解析依赖于模板参数的函数的名称.嗯,这些"时代"之间有什么区别?
class A:
def open_spider(self, spider):
#do some hacking
class B(A):
def open_spider(self, spider):
super(B, self).open_spider(spider)
#something else
Run Code Online (Sandbox Code Playgroud)
现在我希望C调用A的方法而不是B,这至少可以通过两种方式完成:
class C(B):
def open_spider(self, spider):
A.open_spider(self, spider)
#do things
class C(B):
def open_spider(self, spider):
super(B, self).open_spider(spider)
#do things
Run Code Online (Sandbox Code Playgroud) 我想加载尽可能多的数据,这是安全的,这样当前的流程和其他流程一样正常.我宁愿只使用RAM(不使用交换),但欢迎任何建议.可以丢弃过多的数据.这样做的正确方法是什么?如果我等待MemoryException,系统将无法运行(如果使用列表).
data_storage = []
for data in read_next_data():
data_storage.append(data)
Run Code Online (Sandbox Code Playgroud)
数据最终被加载到numpy数组中.
我有一个简单的线性模型,它输入(x,y)对并推导y = b0 + b1 * x中的b0和b1;关键代码如下。它在已知大小的数据集上训练。现在,我想增加不断训练它的能力:即每隔(x,y)添加一次,并根据新数据更新系数。将有无限量的输入。
x = tf.placeholder(tf.float32, [data_len], name="x")
y = ...
b0 = tf.Variable([0.8], trainable=True)
b1 = ...
#the model
y = tf.add(tf.mul(x, b1), b0)
y_act = tf.placeholder(tf.float32, [data_len], name="y_act")
error = tf.sqrt((y - y_act) * (y - y_act))
train_step = tf.train.AdamOptimizer(0.01).minimize(error)
x_in = ...
y_in = ...
init = tf.initialize_all_variables()
sess.run(init)
feed_dict = { ... }
fetches_in = { b0: b0, b1: b1, y: y, train_step: train_step }
for i in range(0, 50):
fetches = …Run Code Online (Sandbox Code Playgroud) #include <iostream>
using namespace std;
template <int fact>
constexpr int pow2T()
{
static_assert(fact < 0, "error");
return fact == 0 ? 1 : pow2T<fact - 1>() * 2;
}
constexpr int e2 = pow2T<2>();
int main(int argc, char *argv[])
{
cout << e2 << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
尝试x^2使用参数静态检查实现静态计算.断言失败..为什么!?
/home/serj/work/untitled/main.cpp:-1:在函数'constexpr int pow2T()[with int fact = 2]'中:
/home/serj/work/untitled/main.cpp:-1:在函数'constexpr int pow2T()[with int fact = 1]'中:
...
python ×6
c++ ×4
casting ×1
http ×1
http-headers ×1
inheritance ×1
logging ×1
memory ×1
numpy ×1
python-3.x ×1
signals ×1
templates ×1
tensorflow ×1