callable

dynamically adding callable to class as instance “method”

和自甴很熟 提交于 2019-12-01 06:31:26
I implemented a metaclass that tears down the class attributes for classes created with it and builds methods from the data from those arguments, then attaches those dynamically created methods directly to the class object (the class in question allows for easy definition of web form objects for use in a web testing framework). It has been working just fine, but now I have a need to add a more complex type of method, which, to try to keep things clean, I implemented as a callable class. Unfortunately, when I try to call the callable class on an instance, it is treated as a class attribute

Callable与Future

限于喜欢 提交于 2019-12-01 06:22:15
接着上一篇继续并发包的学习,本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一个拿到结果。 Callable接口类似于Runnable,从名字就可以看出来了,但是Runnable不会返回结果,并且无法抛出返回结果的异常,而Callable功能更强大一些,被线程执行后,可以返回值,这个返回值可以被Future拿到,也就是说,Future可以拿到异步执行任务的返回值,下面来看一个简单的例子: public class CallableAndFuture { public static void main(String[] args) { Callable<Integer> callable = new Callable<Integer>() { public Integer call() throws Exception { return new Random().nextInt(100); } }; FutureTask<Integer> future = new FutureTask<Integer>(callable); new Thread(future).start(); try { Thread.sleep(5000);// 可能做一些事情 System.out.println(future.get()); } catch

Java: Parameterized Runnable

时光怂恿深爱的人放手 提交于 2019-11-30 22:07:09
问题 Standard Runnable interface has only non-parametrized run() method. There is also Callable<V> interface with call() method returning result of generic type. I need to pass generic parameter, something like this: interface MyRunnable<E> { public abstract void run(E reference); } Is there any standard interface for this purpose or I must declare that basic one by myself? 回答1: Typically you would implement Runnable or Callable as a class that supported a genertic input parameter; e.g. public

'int' object is not callable error in python

拥有回忆 提交于 2019-11-30 20:10:22
I'm getting this error: Traceback (most recent call last): File "C:\Users\George\Desktop\ex3.py", line 15, in <module> s=s+d*2(-1/6.)*(u-1)*(u-2)*(u+2)*(u-4) TypeError: 'int' object is not callable Here is my code: x=input() z=input() n=input() while x>=z: x=input() z=input() while n<0: n=input() while n>0: d=(z-x)/1.*n k=1 s=(d/2.)*((-1/6.)*(x-1)*(x-2)*(x+2)*(x-4)+(-1/6.)*(z-1)*(z-2)*(z+2)*(z-4)) while k<=n-1: u=x+k*d s=s+d*2(-1/6.)*(u-1)*(u-2)*(u+2)*(u-4) k=k+1 print "%.3f" %s x=input() z=input() n=input() if n>0: while x>=z: x=input() z=input() You are trying to use 2 as a function: 2(-1/6.

java Callable FutureTask Excecuter: How to listen to finished task

爱⌒轻易说出口 提交于 2019-11-30 09:07:33
I'm quite new to executer services. Liked doing everything myself, but I think it's time to trust these services. I want to hand by Executer a Runnable . The executer wraps that in a FutureTask and hands it back to me. Now I call poll the done() method. But I would like to be notified when then done() method would return true. There is a get() method that blocks until the Runnable has finished, but then I would need a extra thread for every job, just to see when it's finished. Can I hand my executer some extra Callable to get notified about the task finishing? What's the way to go here? I

dynamically adding callable to class as instance “method”

十年热恋 提交于 2019-11-30 05:44:57
问题 I implemented a metaclass that tears down the class attributes for classes created with it and builds methods from the data from those arguments, then attaches those dynamically created methods directly to the class object (the class in question allows for easy definition of web form objects for use in a web testing framework). It has been working just fine, but now I have a need to add a more complex type of method, which, to try to keep things clean, I implemented as a callable class.

[高并发Java 七] 并发设计模式

 ̄綄美尐妖づ 提交于 2019-11-30 01:14:59
1. 什么是设计模式 在软件工程中,设计模式(design pattern)是对软件设计中普遍存在(反复出现)的各种问题 ,所提出的解决方案。这个术语是由埃里希·伽玛(Erich Gamma)等人在1990年代从建筑设计领 域引入到计算机科学的。 著名的4人帮: Erich Gamma, Richard Helm, Ralph Johnson ,John Vlissides (Gof) 《设计模式:可复用面向对象软件的基础》收录23种模式 2. 单例模式 单例对象的类必须保证只有一个实例存在。许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整体的行为 比如:全局信息配置 单例模式最简单的实现: public class Singleton { private Singleton() { System.out.println("Singleton is create"); } private static Singleton instance = new Singleton(); public static Singleton getInstance() { return instance; } } 由私有构造方法和static来确定唯一性。 缺点:何时产生实例 不好控制 虽然我们知道,在类Singleton第一次被加载的时候,就产生了一个实例。

Basic method chaining

吃可爱长大的小学妹 提交于 2019-11-29 22:20:26
I found this method chaining in python , but even with it I couldn't understand method chaining in Python. Here the goals are two: solve the coding problem and understand method chaining (given that I am still not 100% confident with callables). Down to the problem definition. I want a class that has two methods: one sets a parameter of the object = 'line' and the other overwrites to 'bar'. This is what I got so far: class foo(): def __init__(self, kind=None): self.kind = kind def __call__(self, kind=None): return foo(kind=kind) def my_print(self): print (self.kind) def line(self): return self

How can I terminate Tasks that have timed out in multithreading?

醉酒当歌 提交于 2019-11-29 16:29:27
I need to make a library in which I will have synchronous and asynchronous methods in it. executeSynchronous() - waits until I have a result, returns the result. executeAsynchronous() - returns a Future immediately which can be processed after other things are done, if needed. Core Logic of my Library The customer will use our library and they will call it by passing DataKey builder object. We will then construct a URL by using that DataKey object and make a HTTP client call to that URL by executing it and after we get the response back as a JSON String, we will send that JSON String back to

What is the difference between a function object and a callable object?

我怕爱的太早我们不能终老 提交于 2019-11-29 11:15:27
问题 I recently saw the presentation about the changes in ECMAScript 5. And there was a slide with this statement: Function vs Callable typeof f === 'function' // → f is Callable ({}).toString.call(f) === '[object Function]' // → f is a Function Can anyone explain to me what the difference between Function and Callable is? 回答1: Generally speaking, an object can be callable without being a function. In a language where everything is an object (including functions), callable objects don't have to