我是 React 新手,当我阅读文档时,我发现有两种方法来实现 React 组件,基于函数和基于类。我知道在 React 16.8 之前不可能管理功能组件中的状态,但在那之后有了 React Hooks。
问题是,React Hooks 似乎有一个限制,它们只能在功能组件内部使用。以服务器-客户端为例,isAuthenticated
当收到401时需要改变状态。
//client.js
import { useUserDispatch, signOut } from "auth";
export function request(url, args) {
var dispatch = useUserDispatch();
return fetch(url, args).then(response => {
if (response.status === 401) {
logout(dispatch);
}
}
);
//auth.js
import React from "react";
var UserStateContext = React.createContext();
var UserDispatchContext = React.createContext();
function userReducer(state, action) {
...
}
function UserProvider({ children }) {
var [state, dispatch] = React.useReducer(userReducer, {
isAuthenticated: false,
});
return …
Run Code Online (Sandbox Code Playgroud) 我花了几个小时来研究行为,首先是关于这些问题:
看来,如果在打开文件时使用'O_APPEND'标志,则在Linux上从多个进程登录到同一文件总是可以的。而且我相信python肯定会在其日志记录模块中使用'O_APPEND'标志。
并通过一个小测试:
#!/bin/env python
import os
import logging
logger = logging.getLogger('spam_application')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('spam.log')
logger.addHandler(fh)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
for i in xrange(10000):
p = os.getpid()
logger.debug('Log line number %s in %s', i, p)
Run Code Online (Sandbox Code Playgroud)
我用它运行:
./test.py & ./test.py & ./test.py & ./test.py &
Run Code Online (Sandbox Code Playgroud)
我发现spam.log中没有错。此行为可能支持上述结论。
但是问题来了:
最后,如果两个进程正在同一文件上执行写操作,那是指它们正在调用同一文件上的write(2),谁可以确保来自两个进程的数据不会交织(内核或文件系统?),以及如何交织。[注意:我只想深入了解写系统调用,对此的任何欢迎。
编辑1:
一键验证ip地址失败,文件API.json如下:
{
"$schema": "http://json-schema.org/draft-03/schema#",
"title": "test",
"type": "object",
"properties": {
"type": {"enum": ["spice", "vnc"]},
"listen": {
"type": "string",
"oneOf": [
{"format": "ipv4"},
{"format": "ipv6"}
]
}
},
"additionalProperties": false
}
Run Code Online (Sandbox Code Playgroud)
代码如下:
from jsonschema import Draft3Validator, ValidationError, FormatChecker
import json
if __name__ == '__main__':
graphics1 = {'type': 'spice', 'listen': '0.0.0.0'}
graphics2 = {'type': 'vnc', 'listen': '0.0.0.0'}
graphics3 = {'type': 'abc', 'listen': '0.0.0.0'}
graphics4 = {'type': 'vnc', 'listen': '777.485.999'}
graphics5 = {'type': 'vnc', 'listen': 'fe00::0'}
graphics6 = {'type': 'vnc', 'listen': 'def'}
graphics7 …
Run Code Online (Sandbox Code Playgroud) 我有一个连接到rabbitmq服务器的python程序.该程序启动时,连接良好.但是当rabbitmq服务器重新启动时,我的程序无法重新连接到它,并且只是"Socket closed"(由kombu生成)留下错误,这是没有意义的.
我想知道有关连接失败的详细信息.在服务器端,在rabbitmq日志文件中没有任何用处,它只是说"连接失败"而没有给出任何理由.
我尝试了跟踪插件(https://www.rabbitmq.com/firehose.html),发现当连接失败时,没有向amq.rabbitmq.trace交换发布跟踪信息.我启用了插件:
rabbitmq-plugins enable rabbitmq_tracing
systemctl restart rabbitmq-server
rabbitmqctl trace_on
Run Code Online (Sandbox Code Playgroud)
然后我写了一个客户端来从amq.rabbitmq.trace交换获取消息:
#!/bin/env python
from kombu.connection import BrokerConnection
from kombu.messaging import Exchange, Queue, Consumer, Producer
def on_message(self, body, message):
print("RECEIVED MESSAGE: %r" % (body, ))
message.ack()
def main():
conn = BrokerConnection('amqp://admin:pass@localhost:5672//')
channel = conn.channel()
queue = Queue('debug', channel=channel,durable=False)
queue.bind_to(exchange='amq.rabbitmq.trace', routing_key='publish.amq.rabbitmq.trace')
consumer = Consumer(channel, queue)
consumer.register_callback(on_message)
consumer.consume()
while True:
conn.drain_events()
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
我还尝试从rabbitmq服务器获取一些调试日志.我根据https://www.rabbitmq.com/configure.html重新配置了rabbitmq.config ,并将log_levels设置为
{log_levels, [{connection, info}]}
Run Code Online (Sandbox Code Playgroud)
但结果是rabbitmq服务器无法启动.好像官方文档不适合我,我的rabbitmq服务器版本是3.3.5.然而
{log_levels, [connection,debug,info,error]}
Run Code Online (Sandbox Code Playgroud)
要么
{log_levels, [connection,debug]} …
Run Code Online (Sandbox Code Playgroud) 我想从dart程序中使用libvirt api.这是doc https://www.dartlang.org/articles/native-extensions-for-standalone-dart-vm/.但是我发现它只能在dart命令行app中使用,所以dart.io也是如此.我不熟悉Web应用程序,我想知道为什么会发生这种情况以及为什么我们无法在Web应用程序中调用系统API.这是不可能的还是有其他技巧可以完成这项工作?
编辑:如果我真的想写一个在系统中调用libvirt c api的web应用程序,我该怎么办?对于第一个,我可以编写一个后端服务器来为web应用程序提供api(可能是REST).但是,如果我不想拥有服务器后端,并且可以从chrome扩展访问libvirt api.我看到chrome扩展可以作为后台应用运行,我认为可能不是网络应用.
据我所知,dart的优势似乎是编写Web应用程序.那么为什么命令行应用程序也受支持呢?对于后端代码,dart可以比python更好,还是去?谷歌设计的dart能够支持前端和后端代码,为什么?
我们的目标是一个大系统.我们知道现在必须有一些内存泄漏.但要找到原因是如此困难.每次进程使用的内存达到千兆字节时,它的响应都很慢.从"顶部"看到的cpu使用率是100%,即使这个过程也没有工作要做.
我们使用objgraph和meliae来调试这个问题,没有什么可怀疑的.但是我们发现了一个奇怪的问题,gc.get_objects()得到的对象的总大小与从"top"看到的内存使用量不相等,例如它是50M,但是距离"top"是150M.
有人能给我们指点吗?谢谢.
python ×3
dart ×2
atomic ×1
atomicity ×1
dart-io ×1
ip-address ×1
javascript ×1
jsonschema ×1
libvirt ×1
logging ×1
memory-leaks ×1
mobx ×1
mobx-react ×1
rabbitmq ×1
react-hooks ×1
reactjs ×1