Python: queue method wait_for predicate with arguments












1















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.










share|improve this question





























    1















    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.










    share|improve this question



























      1












      1








      1








      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.










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '18 at 22:38









      Mad Physicist

      35.3k156899




      35.3k156899










      asked Nov 17 '18 at 20:31









      user2442072user2442072

      79212




      79212
























          2 Answers
          2






          active

          oldest

          votes


















          2














          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)





          share|improve this answer


























          • 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





















          0














          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?






          share|improve this answer























            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            2














            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)





            share|improve this answer


























            • 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


















            2














            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)





            share|improve this answer


























            • 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
















            2












            2








            2







            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)





            share|improve this answer















            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)






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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





















            • 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















            0














            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?






            share|improve this answer




























              0














              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?






              share|improve this answer


























                0












                0








                0







                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?






                share|improve this answer













                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?







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 17 '18 at 20:46









                user2442072user2442072

                79212




                79212






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)