handler

python-logging模块

谁都会走 提交于 2019-12-06 14:21:11
一、logging模块简介   logging模块是Python内置的标准模块,主要用于输出运行日志   二、 logging模块使用   # 实例化日志模块     logger = logging.getLogger()   # 定义日志级别,默认是logging.WARNNING     logger.setLevel(logging.INFO)   # 定义处理程序: logPath 日志路径 ,"output.log" 日志文件名     handler = logging.FileHandler(os.path.join( logPath , "output.log"))   # 定义处理程序级别     handler.setLevel(logging.INFO)   # 定义的格式化程序   #参数     %(levelno)s:打印日志级别的数值     %(levelname)s:打印日志级别的名称     %(pathname)s:打印当前执行程序的路径,其实就是sys.argv[0]     %(filename)s:打印当前执行程序名     %(funcName)s:打印日志的当前函数     %(lineno)d:打印日志的当前行号     %(asctime)s:打印日志的时间     %(thread)d:打印线程ID     %

Java threading issue with handler message data being overwritten by next message

一曲冷凌霜 提交于 2019-12-06 14:07:39
I have a thread reading data from a bluetooth stream that sends the data to a handler on the main UIThread as it comes in (based on the Bluetooth Chat Sample ). I've discovered a threading problem that pops up quite frequently. First, some code for reference. BluetoothService.java (Just the part that reads the incomming data stream. It has been set up correctly before this code runs). public void run() { DebugLog.i("BluetoothService", "BEGIN mConnectedThread"); byte[] buffer = new byte[1024]; int bytes; // Keep listening to the InputStream while connected while (true) { try { // Read from the

linux/glibc. Can I use fprintf in signal handler?

主宰稳场 提交于 2019-12-06 14:02:33
Can I use fprintf(stderr) in a signal (SIGALRM) handler with glibc/linux? No you cannot. Check the manpage signal(7) for a list of async-signal-safe functions. fprintf is not included in that list. If you don't need formatting then you can use write(STDERR_FILENO, <buf>, <buflen>) to write to stderr. This is not safe, quoting IBM DeveloperWorks article about Signal Handling Safety Suppose the signal handler prints a message with fprintf and the program was in the middle of an fprintf call using the same stream when the signal was delivered. Both the signal handler's message and the program's

带有额外状态的回调函数

删除回忆录丶 提交于 2019-12-06 13:09:53
编码中常碰到的一个情形是需要编写回调函数,如事件处理函数等。一般的回调函数如常规函数,传递参数,返回计算的值。 def apply_async(func, args, *, callback): # Compute the result result = func(*args) # Invoke the callback with the result callback(result) def print_result(result): print('Got:', result) def add(x, y): return x + y >>> apply_async(add, (2, 3), callback=print_result) Got: 5 >>> apply_async(add, ('hello', 'world'), callback=print_result) Got: helloworld >>> 注意到,print_result()函数仅接受一个参数,即result。 没有其他信息传递。当希望回调函数与环境的其他变量或部分交互时,信息的缺乏有时会带来问题。 在回调中携带额外状态或信息的一种方式是使用绑定的方法而不是一个简单的函数。比如说使用类定义。如下所示: class ResultHandler: def __init__(self): self

Python记录日志

半城伤御伤魂 提交于 2019-12-06 12:38:12
使用logging模块来写日志: 日志直接输出到准备输出: import logging logging.basicConfig(level=logging.WARNING, format="%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s : %(message)s") # 直接使用logging来写日志,会同时写在文件和标准输出中 logging.debug("debug level") logging.info("info level") logging.warning("warning level") logging.error("error level") logging.critical("critical level") 日志输出到文件: import logging logging.basicConfig(level=logging.WARNING, filename='basic_log.txt', filemode='w', format="%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s : %(message)s") # 直接使用logging来写日志,会同时写在文件和标准输出中 logging.debug("debug

回应老赵: 适合C# Actor的消息执行方式 -中看也中用的解决方案

有些话、适合烂在心里 提交于 2019-12-06 12:18:05
     今天粗粗看了老赵的文章 适合C# Actor的消息执行方式 -中看不(3):中用的解决方案 ,我在想如果用我以前写的消息总线来实现那不是中看也中用了,于是顺手写了一个测试代码(具体内容参见 适合C# Actor的消息执行方式 -中看不(3):中用的解决方案 回复),说来很惭愧我的消息总线系列已经一年多没有更新了,我这人太懒散惯了,没办法。废话不多说了,下面我就具体讲解一下设计思路。   在Actor模式中,最重要的就是Actor间的消息发送以及消息路由了。   消息发送:举一个例子实现一个ActorA向ActorB发消息,最简单的方法就是:ActorA中需要内聚或依赖一个ActorB对象,然后就可以直接发消息通讯了,这种方法优点不言而喻- 简单 ,但是当一个系统中存在无数个Actor,一个Actor有可能向N多的其它Actor发消息,这种方法的弊端就暴露无疑了: 强依赖,强耦合。 怎么结局这种问题呢,这时候设计模式中的 中介者模式就可以派上用场了。所有的Actor都向中介者发消息即可,由中介者把消息通过一定的策略路由到特定的Actor,由这个特定的Actor进行处理即可。    消息路由 :消息路由的关键就是路由表,路由表可以用字典来实现,Key-可以用消息类型+Topic来标记即可,Value :就用委托函数即可。 具体简略设计图如下: 类图详解:    

Solr and custom update handler

别说谁变了你拦得住时间么 提交于 2019-12-06 11:58:57
I have a question about Solr and the possibility to implement a customized update handler Basically, the scenario is this: FIELD-A : my main field FIELD-B and FIELD-C : 2 copyfield with source in A After FIELD-A has its value stored, i need this valued to be copied in FIELD-B and C, then processed (let's say extract a substring) and stored in FIELD-B and C before indexing time. I'm not using DIH. edit: i'm pushing my data via nutch (forgot to mention that) As far as i've understood, copyfields triggers after indexing (but i'm not so sure about this). I've already read throu the wiki page and

drf之组件(认证、权限、排序、过滤、分页等)和xadmin、coreapi

不想你离开。 提交于 2019-12-06 10:56:21
认证Authentication 可以在配置文件中配置全局默认的认证方案 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.BasicAuthentication', # 基本认证 'rest_framework.authentication.SessionAuthentication', # session认证 ) } 也可以在每个视图中通过设置authentication_classess属性来设置 from rest_framework.authentication import SessionAuthentication, BasicAuthentication from rest_framework.views import APIView class ExampleView(APIView): authentication_classes = (SessionAuthentication, BasicAuthentication) ... 认证失败会有两种可能的返回值: 401 Unauthorized 未认证 403 Permission Denied 权限被禁止 权限Permissions

Android Handler 引起的内存泄露

二次信任 提交于 2019-12-06 10:27:19
使用Handler导致内存泄露的解决方法 方法一:通过程序逻辑来进行保护。 1.在关闭Activity的时候停掉你的后台线程。线程停掉了,就相当于切断了Handler和外部连接的线,Activity自然会在合适的时候被回收。 2.如果你的Handler是被delay的Message持有了引用,那么使用相应的Handler的removeCallbacks()方法,把消息对象从消息队列移除就行了。 方法二:将Handler声明为静态类。 静态类不持有外部类的对象,所以你的Activity可以随意被回收。代码如下: static class MyHandler extends Handler { @Override public void handleMessage(Message msg) { mImageView.setImageBitmap(mBitmap); } } 但其实没这么简单。使用了以上代码之后,你会发现,由于Handler不再持有外部类对象的引用,导致程序不允许你在Handler中操作Activity中的对象了。所以你需要在Handler中增加一个对Activity的弱引用(WeakReference): static class MyHandler extends Handler { WeakReference<Activity >

How to execute business logic handler in a separate thread pool using netty

老子叫甜甜 提交于 2019-12-06 09:35:57
I have a handler that needs to execute some business logic and I want that to be executed in a separate thread pool to not block the io event loop. I have added DefaultEventExecutorGroup into the pipeline as specified in http://netty.io/4.0/api/io/netty/channel/ChannelPipeline.html javadoc and http://netty.io/wiki/new-and-noteworthy-in-4.0.html#no-more-executionhandler---its-in-the-core wiki: ch.pipeline().addLast(new DefaultEventExecutorGroup(10), new ServerHandler()); Just for testing purposes my ServerHandler just puts the current thread to sleep for 5 seconds: protected void channelRead0