semaphore | Modern UI for Ansible | Continous Integration library
kandi X-RAY | semaphore Summary
kandi X-RAY | semaphore Summary
Ansible Semaphore is a modern UI for Ansible. It lets you easily run Ansible playbooks, get notifications about fails, control access to deployment system. If your project has grown and deploying from the terminal is no longer for you then Ansible Semaphore is what you need. Follow Semaphore on Twitter (AnsibleSem) and StackShare (ansible-semaphore).
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of semaphore
semaphore Key Features
semaphore Examples and Code Snippets
void doWithSemaphore(String key) {
SimultaneousEntriesLockByKey lockByKey = new SimultaneousEntriesLockByKey();
lockByKey.lock(key);
try {
// do stuff
} finally {
lockByKey.unlock(key);
async def fetch_with_sem(sem, session, url, year=None):
async with sem:
return await fetch(url, session, year)
Community Discussions
Trending Discussions on semaphore
QUESTION
I want to check the reachability of about 100 ips addresses and set a limit of concurrent tasks with semaphore. But now I'm not sure how this works exactly or why it doesn't work in the code example. As I could observe the function "task_reachable" is still executed correctly. if no address is reachable, then in the "try_ssh_connection" "all" tasks are executed in parallel and this makes the code incredibly slow.
...ANSWER
Answered 2022-Mar-22 at 14:05Your problem is each running instance of boundary_task
has its own semaphore.
QUESTION
I wrote this program to solve the dining philosophers problem using Dijkstra's algorithm, notice that I'm using an array of booleans (data->locked
) instead of an array of binary semaphores.
I'm not sure if this solution is valid (hence the SO question).
Will access to the data->locked
array in both test
and take_forks
functions cause data races? if so is it even possible to solve this problem using Dijkstra's algorithm with only mutexes?
I'm only allowed to use mutexes, no semaphores, no condition variables (it's an assignment).
Example of usage:
...ANSWER
Answered 2022-Mar-22 at 06:59Your spin loop while (data->locked[i]);
is a data race; you don't hold the lock while reading it data->locked[i]
, and so another thread could take the lock and write to that same variable while you are reading it. In fact, you rely on that happening. But this is undefined behavior.
Immediate practical consequences are that the compiler can delete the test (since in the absence of a data race, data->locked[i]
could not change between iterations), or delete the loop altogether (since it's now an infinite loop, and nontrivial infinite loops are UB). Of course other undesired outcomes are also possible.
So you have to hold the mutex while testing the flag. If it's false, you should then hold the mutex until you set it true and do your other work; otherwise there is a race where another thread could get it first. If it's true, then drop the mutex, wait a little while, take it again, and retry.
(How long is a "little while", and what work you choose to do in between, are probably things you should test. Depending on what kind of fairness algorithms your pthread implementation uses, you might run into situations where take_forks
succeeds in retaking the lock even if put_forks
is also waiting to lock it.)
Of course, in a "real" program, you wouldn't do it this way in the first place; you'd use a condition variable.
QUESTION
I want to download/scrape 50 million log records from a site. Instead of downloading 50 million in one go, I was trying to download it in parts like 10 million at a time using the following code but it's only handling 20,000 at a time (more than that throws an error) so it becomes time-consuming to download that much data. Currently, it takes 3-4 mins to download 20,000 records with the speed of 100%|██████████| 20000/20000 [03:48<00:00, 87.41it/s]
so how to speed it up?
ANSWER
Answered 2022-Feb-27 at 14:37If it's not the bandwidth that limits you (but I cannot check this), there is a solution less complicated than the celery and rabbitmq but it is not as scalable as the celery and rabbitmq, it will be limited by your number of CPU.
Instead of splitting calls on celery workers, you split them on multiple processes.
I modified the fetch
function like this:
QUESTION
While learning about shared process semaphores, I noticed that sem_open() features two function prototypes: https://man7.org/linux/man-pages/man3/sem_open.3.html
...ANSWER
Answered 2022-Mar-01 at 02:53The key is found in the manpage (which you linked):
If
O_CREAT
is specified inoflag
, then two additional arguments must be supplied.
That's exactly the way variadic functions work in C. A variadic function has a fixed number of parameters, each with a well-defined type, and then a ...
, which can be any number of arguments of any type, with the proviso that the called function has to be able to figure out the type of each argument in order to reference it. printf
is indeed the clearest example: the format string includes enough information for printf
to know exactly how many and what type of arguments to expect.
Note that guessing is not permitted. If the function asks for an argument which wasn't provided or asks for a different type than the provided argument, it doesn't get a friendly error indication or a second chance. The result is Undefined Behaviour: anything can happen, including abrupt termination or a nonsensical result. Or a result which seems to make sense but has no basis in reality.
Like open
, sem_open
uses this to allow arguments which are only needed in certain well-defined circumstances. If it is possible that the call will create a new semaphore (or file, in the case of open
), it will request the additional arguments, assuming that they are of the types specified in the documentation.
The caller is responsible for ensuring that the call is correct. Note that since variadic arguments do not have a declared type, the compiler cannot insert type conversions. So if the function is expecting a double
, it cannot be called with an int
, which would normally be possible. Also, if a variadic argument is supposed to be void*
, it must be void*
, and not, for example, 0
(or NULL
), because the automatic conversion from 0 to a null pointer can't be done.
However, the caller is allowed to provide too many arguments, or conversely the called function is not required to examine every argument supplied. (It can't skip over arguments, but it doesn't need to get to the end of the list.)
In retrospect, this may seem like a terrible language feature. But it seemed like a good idea at the time, and it's not going away now.
QUESTION
I am trying to read multiple files in parallel in such a way so that each go routine that is reading a file write its data to that channel, then have a single go-routine that listens to that channel and adds the data to the map. Here is my play.
Below is the example from the play:
...ANSWER
Answered 2022-Feb-24 at 15:18How can I use golang.org/x/sync/errgroup to wait on and handle errors from goroutines or if there is any better way like using semaphore? For example [...] I want to cancels all those remaining in the case of any one routine returning an error (in which case that error is the one bubble back up to the caller). And it should automatically waits for all the supplied go routines to complete successfully for success case.
There are many ways to communicate error states across goroutines. errgroup
does a bunch of heavy lifting though, and is appropriate for this case. Otherwise you're going to end up implementing the same thing.
To use errgroup
we'll need to handle errors (and for your demo, generate some). In addition, to cancel existing goroutines, we'll use a context from errgroup.NewWithContext
.
From the errgroup reference,
Package errgroup provides synchronization, error propagation, and Context cancelation for groups of goroutines working on subtasks of a common task.
Your play doesn't support any error handling. We can't collect and cancel on errors if we don't do any error handling. So I added some code to inject error handling:
QUESTION
I have a lot of tasks in my application. Some tasks depend on others.
For example I have console task that prints outputs from all other "worker" tasks, so all these tasks before printing anything must wait until console task is fully initialized.
I have also more dependencies like this, with kind of dependency tree structure. For example task_modbus needs 3 tasks to be initialized: task_uart, task_filesystem, task_console.
What kind of synchronization object shall I use?
At this moment I'm using semaphores, one for each task dependency relation.
I was thinking about EventGroup, but maybe there is something more lightweight?
...ANSWER
Answered 2022-Feb-16 at 21:34The xEventGroupSync() API seems most appropriate.
QUESTION
I'm using F# and have an AsyncSeq<'t>>
. Each item will take a varying amount of time to process and does I/O that's rate-limited.
I want to run all the operations in parallel and then pass them down the chain as an AsyncSeq<'t>
so I can perform further manipulations on them and ultimately AsyncSeq.fold
them into a final outcome.
The following AsyncSeq
operations almost meet my needs:
mapAsyncParallel
- does the parallelism, but it's unconstrained, (and I don't need the order preserved)iterAsyncParallelThrottled
- parallel and has a max degree of parallelism but doesn't let me return results (and I don't need the order preserved)
What I really need is like a mapAsyncParallelThrottled
. But, to be more precise, really the operation would be entitled mapAsyncParallelThrottledUnordered
.
Things I'm considering:
- use
mapAsyncParallel
but use aSemaphore
within the function to constrain the parallelism myself, which is probably not going to be optimal in terms of concurrency, and due to buffering the results to reorder them. - use
iterAsyncParallelThrottled
and do some ugly folding of the results into an accumulator as they arrive guarded by a lock kinda like this - but I don't need the ordering so it won't be optimal. - build what I need by enumerating the source and emitting results via
AsyncSeqSrc
like this. I'd probably have a set ofAsync.StartAsTask
tasks in flight and start more after eachTask.WaitAny
gives me something toAsyncSeqSrc.put
until I reach themaxDegreeOfParallelism
Surely I'm missing a simple answer and there's a better way?
Failing that, would love someone to sanity check my option 3 in either direction!
I'm open to using AsyncSeq.toAsyncEnum
and then use an IAsyncEnumerable
way of achieving the same outcome if that exists, though ideally without getting into TPL DataFlow or RX land if it can be avoided (I've done extensive SO searching for that without results...).
ANSWER
Answered 2022-Feb-10 at 10:35If I'm understanding your requirements then something like this will work. It effectively combines the iter unordered with a channel to allow a mapping instead.
QUESTION
Starting in iOS13, one can monitor the progress of an OperationQueue
using the progress
property. The documentation states that only operations that do not override start()
count when tracking progress. However, asynchronous operations must override start()
and not call super()
according to the documentation.
Does this mean asynchronous
operations and progress
are mutually exclusive (i.e. only synchronous operations can be used with progress)? This seems like a massive limitation if this is the case.
In my own project, I removed my override of start()
and everything appears to work okay (e.g. dependencies are only started when isFinished
is set to true
on the dependent operation internally in my async operation base class). BUT, this seems risky since Operation
explicitly states to override start()
.
Thoughts?
Documentaiton references:
https://developer.apple.com/documentation/foundation/operationqueue/3172535-progress
By default, OperationQueue doesn’t report progress until totalUnitCount is set. When totalUnitCount is set, the queue begins reporting progress. Each operation in the queue contributes one unit of completion to the overall progress of the queue for operations that are finished by the end of main(). Operations that override start() and don’t invoke super don’t contribute to the queue’s progress.
https://developer.apple.com/documentation/foundation/operation/1416837-start
If you are implementing a concurrent operation, you must override this method and use it to initiate your operation. Your custom implementation must not call super at any time. In addition to configuring the execution environment for your task, your implementation of this method must also track the state of the operation and provide appropriate state transitions.
Update: I ended up ditching my AysncOperation
for a simple SyncOperation
that waits until finish()
is called (using a semaphore).
ANSWER
Answered 2022-Feb-03 at 20:53You are combining two different but related concepts; asynchronous and concurrency.
An OperationQueue
always dispatches Operations
onto a separate thread so you do not need to make them explicitly make them asynchronous and there is no need to override start()
. You should ensure that your main()
does not return until the operation is complete. This means blocking if you perform asynchronous tasks such as network operations.
It is possible to execute an Operation
directly. In the case where you want concurrent execution of those operations you need to make them asynchronous. It is in this situation that you would override start()
If you want to implement a concurrent operation—that is, one that runs asynchronously with respect to the calling thread—you must write additional code to start the operation asynchronously. For example, you might spawn a separate thread, call an asynchronous system function, or do anything else to ensure that the start method starts the task and returns immediately and, in all likelihood, before the task is finished.
Most developers should never need to implement concurrent operation objects. If you always add your operations to an operation queue, you do not need to implement concurrent operations. When you submit a nonconcurrent operation to an operation queue, the queue itself creates a thread on which to run your operation. Thus, adding a nonconcurrent operation to an operation queue still results in the asynchronous execution of your operation object code. The ability to define concurrent operations is only necessary in cases where you need to execute the operation asynchronously without adding it to an operation queue.
In summary, make sure your operations are synchronous and do not override start
if you want to take advantage of progress
Update
While the normal advice is not to try and make asynchronous tasks synchronous, in this case it is the only thing you can do if you want to take advantage of progress
. The problem is that if you have an asynchronous operation, the queue cannot tell when it is actually complete. If the queue can't tell when an operation is complete then it can't update progress
accurately for that operation.
You do need to consider the impact on the thread pool of doing this.
The alternative is not to use the inbuilt progress
feature and create your own property that you update from your tasks.
QUESTION
Is there a way in C# rx to handle backpressure? I'm trying to call a web api from the results of a paged query. This web api is very fragile and I need to not have more than say 3 concurrent calls, so, the program should be something like:
- Feth a page from db
- Call the web api with a maximum of three concurrent calls per each record on the page
- Save the results back to db
- Fetch another page and repeat until there are no more results.
I'm not really getting the sequence that I'm after, basically the db gets all the records regardless of whether they can be processed or not.
I've tried a variety of things including tweaking at the ObserveOn
operator, implementing a semaphore, and a few other things. Could I get a little bit of guidance to implement something like this?
ANSWER
Answered 2022-Jan-13 at 12:01The Rx does not support backpressure, so there is no easy way to fetch the records from the DB at the same tempo that the records are processed. Maybe you could use a Subject
as a signaling mechanism, push a value every time a record is processed, and devise a way to use these signals at the producing site to fetch a new record from the DB when a signal is received. But it will be a messy and idiomatic solution. The TPL Dataflow is a more suitable tool than the Rx for doing this kind of work. It supports natively the BoundedCapacity
configuration option.
Some comments regarding the code you've posted, that are not directly related to the backpressure issue:
The Merge
operator with a maxConcurrent
parameter imposes a limit on the concurrent subscriptions to the inner sequences, but this will have no effect in case the inner sequences are already up and running. So you have to ensure that the inner sequences are cold, and a handy way to do this is the Defer
operator:
QUESTION
class SomeViewController: UIViewController {
let semaphore = DispatchSemaphore(value: 1)
deinit {
semaphore.signal() // just in case?
}
func someLongAsyncTask() {
semaphore.wait()
...
semaphore.signal() // called much later
}
}
...ANSWER
Answered 2021-Dec-23 at 18:29You have stumbled into a feature/bug in DispatchSemaphore
. If you look at the stack trace and jump to the top of the stack, you'll see assembly with a message:
BUG IN CLIENT OF LIBDISPATCH: Semaphore object deallocated while in use
E.g.,
This is because DispatchSemaphore
checks to see whether the semaphore’s associated value
is less at deinit
than at init
, and if so, it fails. In short, if the value is less, libDispatch concludes that the semaphore is still being used.
This may appear to be overly conservative, as this generally happens if the client was sloppy, not because there is necessarily some serious problem. And it would be nice if it issued a meaningful exception message rather forcing us to dig through stack traces. But that is how libDispatch works, and we have to live with it.
All of that having been said, there are three possible solutions:
You obviously have a path of execution where you are
wait
ing and not reaching thesignal
before the object is being deallocated. Change the code so that this cannot happen and your problem goes away.While you should just make sure that
wait
andsignal
calls are balanced (fixing the source of the problem), you can use the approach in your question (to address the symptoms of the problem). But thatdeinit
approach solves the problem through the use of non-local reasoning. If you change the initialization, so the value is, for example, five, you or some future programmer have to remember to also go todeinit
and insert four moresignal
calls.The other approach is to instantiate the semaphore with a value of zero and then, during initialization, just
signal
enough times to get the value up to where you want it. Then you won’t have this problem. This keeps the resolution of the problem localized in initialization rather than trying to have to remember to adjustdeinit
every time you change the non-zero value during initialization.See https://lists.apple.com/archives/cocoa-dev/2014/Apr/msg00483.html.
Itai enumerated a number of reasons that one should not use semaphores at all. There are lots of other reasons, too:
- Semaphores are incompatible with new Swift concurrency system (see Swift concurrency: Behind the scenes);
- Semaphores can also easily introduce deadlocks if not precise in one’s code;
- Semaphores are generally antithetical to cancellable asynchronous routines; etc.
Nowadays, semaphores are almost always the wrong solution. If you tell us what problem you are trying to solve with the semaphore, we might be able to recommend other, better, solutions.
You said:
However, if the async function returns before
deinit
is called and the view controller is deinitialized, thensignal()
is called twice, which doesn't seem problematic. But is it safe and/or wise to do this?
Technically speaking, over-signaling does not introduce new problems, so you don't really have to worry about that. But this “just in case” over-signaling does have a hint of code smell about it. It tells you that you have cases where you are waiting but never reaching signaling, which suggests a logic error (see point 1 above).
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install semaphore
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page