concurrent-programming

How to print results of Python ThreadPoolExecutor.map immediately?

…衆ロ難τιáo~ 提交于 2019-12-06 06:07:25
I am running a function for several sets of iterables, returning a list of all results as soon as all processes are finished. def fct(variable1, variable2): # do an operation that does not necessarily take the same amount of # time for different input variables and yields result1 and result2 return result1, result2 variables1 = [1,2,3,4] variables2 = [7,8,9,0] with ThreadPoolExecutor(max_workers = 8) as executor: future = executor.map(fct,variables1,variables2) print '[%s]' % ', '.join(map(str, future)) >>> [ (12,3) , (13,4) , (14,5) , (15,6) ] How can I print intermediary results e.g. for

why doesn't this erlang code work?

最后都变了- 提交于 2019-12-06 06:00:58
fib(N)-> P1 = spawn(fun concFib:conFib/0), P2 = spawn(fun concFib:conFib/0), X=rpc(P1,N-2),Y=rpc(P2,N-1),X+Y. conFib()-> receive {Client,N} -> Client ! regfib(N) end. rpc(Pid,Request)-> case erlang:is_process_alive(Pid) of true -> begin Pid ! {self(),Request}, receive {Pid,Respond} -> Respond end end; false -> io:format("~w process is dead.",[Pid]) end. regfib(N)-> case N<2 of true -> 1; false -> regfib(N,1,1,1) end. regfib(N,N,X,_)-> X ; regfib(N,M,X,Y)-> regfib(N,M+1,X+Y,X). The idea is to divide the fib(N) process into two process one calculates fib(N-2) and the other one calc. fib(N-1)

Pattern for Java ConcurrentHashMap of Sets

不想你离开。 提交于 2019-12-06 05:11:45
问题 A data structure that I use commonly in multi-threaded applications is a ConcurrentHashMap where I want to save a group of items that all share the same key. The problem occurs when installing the first item for a particular key value. The pattern that I have been using is: final ConcurrentMap<KEYTYPE, Set<VALUETYPE>> hashMap = new ConcurrentHashMap<KEYTYPE, Set<VALUETYPE>>(); // ... Set<VALUETYPE> newSet = new HashSet<VALUETYPE>(); final Set<VALUETYPE> set = hashMap.putIfAbsent(key, newSet)

C#/CLR: MemoryBarrier and torn reads

本秂侑毒 提交于 2019-12-06 03:33:25
问题 Just playing around with concurrency in my spare time, and wanted to try preventing torn reads without using locks on the reader side so concurrent readers don't interfere with each other. The idea is to serialize writes via a lock, but use only a memory barrier on the read side. Here's a reusable abstraction that encapsulate the approach I came up with: public struct Sync<T> where T : struct { object write; T value; int version; // incremented with each write public static Sync<T> Create() {

What's the different between LinkedBlockingQueue and ConcurrentLinkedQueue?

一世执手 提交于 2019-12-05 01:46:21
问题 I've read the blog, but i'm not sure whether his conclusion is correct : http://www.javacodegeeks.com/2010/09/java-best-practices-queue-battle-and.html#ixzz1seaiSLwp He said : As you can see from the provided performance results LinkedBlockingQueue achieved the best combined (adding and removing elements) performance results and should be your number one candidate for implementing producer – consumer schenarios. I wonder that, doen't it faster if i don't use lock in my code ? So why the

Code examples that use fine grained locks (JCR Jackrabbit?)

纵饮孤独 提交于 2019-12-04 20:12:43
I'm doing an academic research in trying to develop a programming tool that assists in implementing fine-grained locking functions, for concurrent programs that maintain tree-like data structures. For example, the programmer may write some functions that receive a tree root-node and modify the tree (by traversing on some routes and adding/removing nodes), and the tool will help him to find where in the code nodes should be locked and where they can be released - so the functions could be executed concurrently on the same tree. I am looking for some real-life code examples in which such fine

When to choose multithreading or multiprocessing? [closed]

北战南征 提交于 2019-12-04 09:42:35
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I've never done something on concurrent programming.What I know about them is only from OS books. And I met this question on an interview today. I wonder if anybody can give me an intuitive explanation on multithread and multiprocess and when to choose them. Or,maybe you can recommend me some books or links with

All goroutines are asleep - deadlock! ----— Error

人走茶凉 提交于 2019-12-03 17:37:27
I want to write three concurrent go routines that sends integers to each other. Now, my code is compiled properly, however after first execution it gives error "all goroutines are asleep - deadlock!". I tried to find the error but I could not able to find any error in code logic.Can anybody help me to find the mistake with my code. My code is given below. Thanks in advance. package main import "rand" func Routine1(command12 chan int, response12 chan int, command13 chan int, response13 chan int) { for i := 0; i < 10; i++ { y := rand.Intn(10) if y%2 == 0 { command12 <- y } if y%2 != 0 {

What's the different between LinkedBlockingQueue and ConcurrentLinkedQueue?

限于喜欢 提交于 2019-12-03 16:16:05
I've read the blog, but i'm not sure whether his conclusion is correct : http://www.javacodegeeks.com/2010/09/java-best-practices-queue-battle-and.html#ixzz1seaiSLwp He said : As you can see from the provided performance results LinkedBlockingQueue achieved the best combined (adding and removing elements) performance results and should be your number one candidate for implementing producer – consumer schenarios. I wonder that, doen't it faster if i don't use lock in my code ? So why the LinkedBlockingQueue is faster than the lock-free Queue(ConcurrentLinkedQueue) ? Thanks !

How to Stop a Running a Program Using Other Java Program

与世无争的帅哥 提交于 2019-12-02 08:33:35
问题 I have been implementing a program to compile and run other applications. I was wondering if there is a way to terminate a program when my application discovers that there is an issue e.g. infinite loop. I tried to using process.Destroy() but it kills the CMD not that actual program that has infinite loop... Your help is really appreciated. Here is a part of my code: synchronized (pro) { pro.wait(30000); } try{ pro.exitValue(); }catch (IllegalThreadStateException ex) { pro.destroy();