Python: queue method wait_for predicate with arguments
I'm trying to utilize the wait_for
method of the queue module. I have a callable for the predicate, and it works if I don't pass any arguments, however the callable requires an int
argument.
WORKS:
self.cv.wait_for(fn, timeout=5.0)
FAILS:
self.cv.wait_for(fn(1), timeout5.0)
This generates the error "bool is not callable".
I've tried the following:
self.cv.wait_for((fn(1)) , timeout=5.0)
Result: bool is not callable
self.cv.wait_for((fn, 1), timeout=5.0)
Result: tuple object is not callable
self.cv.wait_for((fn, 1)(), timeout=5.0)
Result: tuple object is not callable
self.cv.wait_for((fn(), 1), timeout=5.0)
Result: fn missing 1 required positional argument: 'int'
fn is a simple function just for testing.
fn
:
def fn(int):
if int:
return True
else:
return False
Any guidance is greatly appreciated.
python python-3.x
add a comment |
I'm trying to utilize the wait_for
method of the queue module. I have a callable for the predicate, and it works if I don't pass any arguments, however the callable requires an int
argument.
WORKS:
self.cv.wait_for(fn, timeout=5.0)
FAILS:
self.cv.wait_for(fn(1), timeout5.0)
This generates the error "bool is not callable".
I've tried the following:
self.cv.wait_for((fn(1)) , timeout=5.0)
Result: bool is not callable
self.cv.wait_for((fn, 1), timeout=5.0)
Result: tuple object is not callable
self.cv.wait_for((fn, 1)(), timeout=5.0)
Result: tuple object is not callable
self.cv.wait_for((fn(), 1), timeout=5.0)
Result: fn missing 1 required positional argument: 'int'
fn is a simple function just for testing.
fn
:
def fn(int):
if int:
return True
else:
return False
Any guidance is greatly appreciated.
python python-3.x
add a comment |
I'm trying to utilize the wait_for
method of the queue module. I have a callable for the predicate, and it works if I don't pass any arguments, however the callable requires an int
argument.
WORKS:
self.cv.wait_for(fn, timeout=5.0)
FAILS:
self.cv.wait_for(fn(1), timeout5.0)
This generates the error "bool is not callable".
I've tried the following:
self.cv.wait_for((fn(1)) , timeout=5.0)
Result: bool is not callable
self.cv.wait_for((fn, 1), timeout=5.0)
Result: tuple object is not callable
self.cv.wait_for((fn, 1)(), timeout=5.0)
Result: tuple object is not callable
self.cv.wait_for((fn(), 1), timeout=5.0)
Result: fn missing 1 required positional argument: 'int'
fn is a simple function just for testing.
fn
:
def fn(int):
if int:
return True
else:
return False
Any guidance is greatly appreciated.
python python-3.x
I'm trying to utilize the wait_for
method of the queue module. I have a callable for the predicate, and it works if I don't pass any arguments, however the callable requires an int
argument.
WORKS:
self.cv.wait_for(fn, timeout=5.0)
FAILS:
self.cv.wait_for(fn(1), timeout5.0)
This generates the error "bool is not callable".
I've tried the following:
self.cv.wait_for((fn(1)) , timeout=5.0)
Result: bool is not callable
self.cv.wait_for((fn, 1), timeout=5.0)
Result: tuple object is not callable
self.cv.wait_for((fn, 1)(), timeout=5.0)
Result: tuple object is not callable
self.cv.wait_for((fn(), 1), timeout=5.0)
Result: fn missing 1 required positional argument: 'int'
fn is a simple function just for testing.
fn
:
def fn(int):
if int:
return True
else:
return False
Any guidance is greatly appreciated.
python python-3.x
python python-3.x
edited Nov 17 '18 at 22:38
Mad Physicist
35.3k156899
35.3k156899
asked Nov 17 '18 at 20:31
user2442072user2442072
79212
79212
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Condition.wait_for
accepts a predicate that is a callable that accepts no arguments. The return value of the predicate can be anything since all objects have a boolean interpretation.
self.test
is a callable that accepts one argument, so it is not a suitable predicate. self.test(1)
is the object that is the result of calling the method, which is a bool
, not a callable.
Your approach with the lambda
is the simplest and easiest in this case:
self.cv.wait_for(lambda: self.test(1), timeout=5.0)
If your function is complex enough, you can have it return the predicate callable with appropriate refactoring, instead of wrapping it in a lambda. For example:
def fn(i):
def preficate():
return bool(i)
return predicate
...
self.cv.wait_for(fn(1), timeout=5.0)
Thank you, it's greatly appreciated. This raises another question, which I've asked here: link if you're willing.
– user2442072
Nov 17 '18 at 23:34
@user2442072 Glad that helped. An upvote would be nice. I'll be happy to see the link. Good on you for asking another question instead of asking for scope creep.
– Mad Physicist
Nov 17 '18 at 23:41
add a comment |
I was able to solve it by using a lambda:
self.cv.wait_for( lambda: self.test(1), timeout=5.0)
But I'm curious as to if there are other ways?
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355297%2fpython-queue-method-wait-for-predicate-with-arguments%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Condition.wait_for
accepts a predicate that is a callable that accepts no arguments. The return value of the predicate can be anything since all objects have a boolean interpretation.
self.test
is a callable that accepts one argument, so it is not a suitable predicate. self.test(1)
is the object that is the result of calling the method, which is a bool
, not a callable.
Your approach with the lambda
is the simplest and easiest in this case:
self.cv.wait_for(lambda: self.test(1), timeout=5.0)
If your function is complex enough, you can have it return the predicate callable with appropriate refactoring, instead of wrapping it in a lambda. For example:
def fn(i):
def preficate():
return bool(i)
return predicate
...
self.cv.wait_for(fn(1), timeout=5.0)
Thank you, it's greatly appreciated. This raises another question, which I've asked here: link if you're willing.
– user2442072
Nov 17 '18 at 23:34
@user2442072 Glad that helped. An upvote would be nice. I'll be happy to see the link. Good on you for asking another question instead of asking for scope creep.
– Mad Physicist
Nov 17 '18 at 23:41
add a comment |
Condition.wait_for
accepts a predicate that is a callable that accepts no arguments. The return value of the predicate can be anything since all objects have a boolean interpretation.
self.test
is a callable that accepts one argument, so it is not a suitable predicate. self.test(1)
is the object that is the result of calling the method, which is a bool
, not a callable.
Your approach with the lambda
is the simplest and easiest in this case:
self.cv.wait_for(lambda: self.test(1), timeout=5.0)
If your function is complex enough, you can have it return the predicate callable with appropriate refactoring, instead of wrapping it in a lambda. For example:
def fn(i):
def preficate():
return bool(i)
return predicate
...
self.cv.wait_for(fn(1), timeout=5.0)
Thank you, it's greatly appreciated. This raises another question, which I've asked here: link if you're willing.
– user2442072
Nov 17 '18 at 23:34
@user2442072 Glad that helped. An upvote would be nice. I'll be happy to see the link. Good on you for asking another question instead of asking for scope creep.
– Mad Physicist
Nov 17 '18 at 23:41
add a comment |
Condition.wait_for
accepts a predicate that is a callable that accepts no arguments. The return value of the predicate can be anything since all objects have a boolean interpretation.
self.test
is a callable that accepts one argument, so it is not a suitable predicate. self.test(1)
is the object that is the result of calling the method, which is a bool
, not a callable.
Your approach with the lambda
is the simplest and easiest in this case:
self.cv.wait_for(lambda: self.test(1), timeout=5.0)
If your function is complex enough, you can have it return the predicate callable with appropriate refactoring, instead of wrapping it in a lambda. For example:
def fn(i):
def preficate():
return bool(i)
return predicate
...
self.cv.wait_for(fn(1), timeout=5.0)
Condition.wait_for
accepts a predicate that is a callable that accepts no arguments. The return value of the predicate can be anything since all objects have a boolean interpretation.
self.test
is a callable that accepts one argument, so it is not a suitable predicate. self.test(1)
is the object that is the result of calling the method, which is a bool
, not a callable.
Your approach with the lambda
is the simplest and easiest in this case:
self.cv.wait_for(lambda: self.test(1), timeout=5.0)
If your function is complex enough, you can have it return the predicate callable with appropriate refactoring, instead of wrapping it in a lambda. For example:
def fn(i):
def preficate():
return bool(i)
return predicate
...
self.cv.wait_for(fn(1), timeout=5.0)
edited Nov 17 '18 at 23:40
answered Nov 17 '18 at 22:37
Mad PhysicistMad Physicist
35.3k156899
35.3k156899
Thank you, it's greatly appreciated. This raises another question, which I've asked here: link if you're willing.
– user2442072
Nov 17 '18 at 23:34
@user2442072 Glad that helped. An upvote would be nice. I'll be happy to see the link. Good on you for asking another question instead of asking for scope creep.
– Mad Physicist
Nov 17 '18 at 23:41
add a comment |
Thank you, it's greatly appreciated. This raises another question, which I've asked here: link if you're willing.
– user2442072
Nov 17 '18 at 23:34
@user2442072 Glad that helped. An upvote would be nice. I'll be happy to see the link. Good on you for asking another question instead of asking for scope creep.
– Mad Physicist
Nov 17 '18 at 23:41
Thank you, it's greatly appreciated. This raises another question, which I've asked here: link if you're willing.
– user2442072
Nov 17 '18 at 23:34
Thank you, it's greatly appreciated. This raises another question, which I've asked here: link if you're willing.
– user2442072
Nov 17 '18 at 23:34
@user2442072 Glad that helped. An upvote would be nice. I'll be happy to see the link. Good on you for asking another question instead of asking for scope creep.
– Mad Physicist
Nov 17 '18 at 23:41
@user2442072 Glad that helped. An upvote would be nice. I'll be happy to see the link. Good on you for asking another question instead of asking for scope creep.
– Mad Physicist
Nov 17 '18 at 23:41
add a comment |
I was able to solve it by using a lambda:
self.cv.wait_for( lambda: self.test(1), timeout=5.0)
But I'm curious as to if there are other ways?
add a comment |
I was able to solve it by using a lambda:
self.cv.wait_for( lambda: self.test(1), timeout=5.0)
But I'm curious as to if there are other ways?
add a comment |
I was able to solve it by using a lambda:
self.cv.wait_for( lambda: self.test(1), timeout=5.0)
But I'm curious as to if there are other ways?
I was able to solve it by using a lambda:
self.cv.wait_for( lambda: self.test(1), timeout=5.0)
But I'm curious as to if there are other ways?
answered Nov 17 '18 at 20:46
user2442072user2442072
79212
79212
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355297%2fpython-queue-method-wait-for-predicate-with-arguments%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown