concurrency

How to properly create and run concurrent tasks using python's asyncio module?

痴心易碎 提交于 2019-12-28 07:31:30
问题 I am trying to properly understand and implement two concurrently running Task objects using Python 3's relatively new asyncio module. In a nutshell, asyncio seems designed to handle asynchronous processes and concurrent Task execution over an event loop. It promotes the use of await (applied in async functions) as a callback-free way to wait for and use a result, without blocking the event loop. (Futures and callbacks are still a viable alternative.) It also provides the asyncio.Task() class

NewThreadScheduler.Default schedules all work on same thread

岁酱吖の 提交于 2019-12-28 06:56:06
问题 I'm currently trying to wrap my head around concurrency with RX .NET and getting confused by something. I want to run four relatively slow tasks in parallel, so I assumed NewThreadScheduler.Default would be the way to go, as it "Represents an object that schedules each unit of work on a separate thread." . Here's my setup code: static void Test() { Console.WriteLine("Starting. Thread {0}", Thread.CurrentThread.ManagedThreadId); var query = Enumerable.Range(1, 4); var obsQuery = query

MYSQL last_insert_id() and concurrency

别来无恙 提交于 2019-12-28 06:55:29
问题 I have a simple MYSQL question. If I make a query that contains LAST_INSERT_ID() right after an INSERT QUERY running on a web page which has many concurrent users accessing other pages that perform INSERT operations would the value of LAST_INSERT_ID() be adulterated/corrupted? 回答1: No, it will return the insert id from the current connection. As long as your script hasn't made any other inserts, you will get the one you want. Also be aware that this will only return a generated ID (e.g. an

Concurrent Set Queue

Deadly 提交于 2019-12-28 05:56:21
问题 Maybe this is a silly question, but I cannot seem to find an obvious answer. I need a concurrent FIFO queue that contains only unique values. Attempting to add a value that already exists in the queue simply ignores that value. Which, if not for the thread safety would be trivial. Is there a data structure in Java or maybe a code snipit on the interwebs that exhibits this behavior? 回答1: If you want better concurrency than full synchronization, there is one way I know of to do it, using a

TPL Dataflow, whats the functional difference between Post() and SendAsync()?

大城市里の小女人 提交于 2019-12-28 05:35:09
问题 I am confused about the difference between sending items through Post() or SendAsync(). My understanding is that in all cases once an item reached the input buffer of a data block, control is returned to the calling context, correct? Then why would I ever need SendAsync? If my assumption is incorrect then I wonder, on the contrary, why anyone would ever use Post() if the whole idea of using data blocks is to establish a concurrent and async environment. I understand of course the difference

Are C# structs thread safe?

大憨熊 提交于 2019-12-28 04:09:07
问题 Is a C# struct thread-safe? For example if there is a: struct Data { int _number; public int Number { get { return _number; } set { _number = value; } } public Data(int number) { _number = number; } } in another type: class DadData { public Data TheData { get; set; } } is property named TheData, thread-safe? 回答1: No, structures in .NET are not intrinsically thread-safe. However, the copy-by-value semantics that structures have great relevance to this converation. If you are passing your

How to declare array elements volatile in Java?

旧时模样 提交于 2019-12-28 03:45:09
问题 Is there a way to declare array elements volatile in Java? I.e. volatile int[] a = new int[10]; declares the array reference volatile , but the array elements (e.g. a[1] ) are still not volatile. So I'm looking for something like volatile int[] a = new volatile int[10]; but it doesn't work that way. Is it possible at all? 回答1: Use AtomicIntegerArray or AtomicLongArray or AtomicReferenceArray The AtomicIntegerArray class implements an int array whose individual fields can be accessed with

Thread Confinement

不羁岁月 提交于 2019-12-28 03:39:07
问题 I am reading Java Concurrency in Practice and kind of confused with the thread confinement concept. The book says that When an object is confined to a thread, such usage is automatically thread-safe even if the confined object itself is not So when an object is confined to a thread, no other thread can have access to it? Is that what it means to be confined to a thread? How does one keep an object confined to a thread? Edit: But what if I still want to share the object with another thread?

Are 64 bit assignments in Java atomic on a 32 bit machine?

拥有回忆 提交于 2019-12-28 03:36:36
问题 If I have code like this - long x; x = 0xFFFFFFFFL; If i run this code on a 32 bit machine is it guaranteed to be atomic or is it possible that a different thread reading x, might get an incomplete/garbage value? 回答1: Here is the short summary: For references, reads/writes are ALWAYS atomic (even in 64 bit implementations!) For int , char , byte , short , boolean , float , reads/writes are ALWAYS atomic For double and long , if they're volatile , reads/writes are ALWAYS atomic Therefore there

What's the use of the SyncRoot pattern?

蓝咒 提交于 2019-12-28 03:29:28
问题 I'm reading a c# book that describes the SyncRoot pattern. It shows void doThis() { lock(this){ ... } } void doThat() { lock(this){ ... } } and compares to the SyncRoot pattern: object syncRoot = new object(); void doThis() { lock(syncRoot ){ ... } } void doThat() { lock(syncRoot){ ... } } However, I don't really understand the difference here; it seems that in both cases both methods can only be accessed by one thread at a time. The book describes ... because the object of the instance can