multiprocessing help: launching Loops2019 Community Moderator ElectionAccessing the index in 'for' loops?python multiprocessing vs threading for cpu bound work on windows and linuxMultiprocessing vs Threading PythonIterating over dictionaries using 'for' loopsPython multiprocessing pool.map for multiple argumentsWhat kind of problems (if any) would there be combining asyncio with multiprocessing?Python Sync Variables in multiprocessingHow to read all message from queue using stomp library in Python?How to execute code just before terminating the process in python?Python in Linux child process not detecting changes in dictionary in parent process

Why aren't there more Gauls like Obelix?

Exempt portion of equation line from aligning?

PTIJ: Sport in the Torah

Why do we say 'Pairwise Disjoint', rather than 'Disjoint'?

How do you use environments that have the same name within a single latex document?

An Undercover Army

Why does this boat have a landing pad? (SpaceX's GO Searcher) Any plans for propulsive capsule landings?

Giving a career talk in my old university, how prominently should I tell students my salary?

After Brexit, will the EU recognize British passports that are valid for more than ten years?

Was it really inappropriate to write a pull request for the company I interviewed with?

Are small insurances worth it?

How can I have x-axis ticks that show ticks scaled in powers of ten?

Help! My Character is too much for her story!

Why do phishing e-mails use faked e-mail addresses instead of the real one?

Was this cameo in Captain Marvel computer generated?

Can the Witch Sight warlock invocation see through the Mirror Image spell?

Interpretation of linear regression interaction term plot

Generating a list with duplicate entries

Why does a car's steering wheel get lighter with increasing speed

Is there a logarithm base for which the logarithm becomes an identity function?

Inorganic chemistry handbook with reaction lists

Why do we call complex numbers “numbers” but we don’t consider 2-vectors numbers?

What is the oldest European royal house?

The (Easy) Road to Code



multiprocessing help: launching Loops



2019 Community Moderator ElectionAccessing the index in 'for' loops?python multiprocessing vs threading for cpu bound work on windows and linuxMultiprocessing vs Threading PythonIterating over dictionaries using 'for' loopsPython multiprocessing pool.map for multiple argumentsWhat kind of problems (if any) would there be combining asyncio with multiprocessing?Python Sync Variables in multiprocessingHow to read all message from queue using stomp library in Python?How to execute code just before terminating the process in python?Python in Linux child process not detecting changes in dictionary in parent process










0















What I have:

Web application that does some things, running on Bottle microframework, here is the Main



def run_server():
bottle.run(host=env.str('SRVRIP'), port=env.str('SRVRPORT'), debug=False)

# Main
if __name__ == '__main__':
run_server()




What I want to add:

I want to split this app in 2 processes: first called main_service() which simply runs the server, second called backup_service() which has to take a periodic Db backup (once per hour)





What I've tried

First I've tried with the asyncio module, failing miserably (tried just for fun and to understand something new).
Now I'm facing on multiprocessing (leaving aside threading cause I don't want GIL troubles), but I can't understand how to make it work, here is my code:



import multiprocessing
import time
import os


def main_service():
print(f'Process os.getpid()')

while(True):
sentence=input()
print(f'USER: sentence')


def backup_service():
print(f'Process os.getpgid()')

while(True):
print('BACKUP')
time.sleep(1)


if __name__ == "__main__":
processes = []

t = multiprocessing.Process(target=main_service())
processes.append(t)
t.start()

t = multiprocessing.Process(target=backup_service())
processes.append(t)
t.start()

for process in processes:
process.join()

print('Done')


If run


Process 14608

As you can see

USER: As you can see

It asks for input

USER: It asks for input

But backup_service() is not printing anything

USER: But backup_service() is not printing anything



Expected output


Process 14608

While I'm writing

USER: While I'm writing

BACKUP

every second

USER: every second

backup_service() has to print 'BACKUP'
USER: backup_service() has to print 'BACKUP'
BACKUP



Can anyone help?










share|improve this question




























    0















    What I have:

    Web application that does some things, running on Bottle microframework, here is the Main



    def run_server():
    bottle.run(host=env.str('SRVRIP'), port=env.str('SRVRPORT'), debug=False)

    # Main
    if __name__ == '__main__':
    run_server()




    What I want to add:

    I want to split this app in 2 processes: first called main_service() which simply runs the server, second called backup_service() which has to take a periodic Db backup (once per hour)





    What I've tried

    First I've tried with the asyncio module, failing miserably (tried just for fun and to understand something new).
    Now I'm facing on multiprocessing (leaving aside threading cause I don't want GIL troubles), but I can't understand how to make it work, here is my code:



    import multiprocessing
    import time
    import os


    def main_service():
    print(f'Process os.getpid()')

    while(True):
    sentence=input()
    print(f'USER: sentence')


    def backup_service():
    print(f'Process os.getpgid()')

    while(True):
    print('BACKUP')
    time.sleep(1)


    if __name__ == "__main__":
    processes = []

    t = multiprocessing.Process(target=main_service())
    processes.append(t)
    t.start()

    t = multiprocessing.Process(target=backup_service())
    processes.append(t)
    t.start()

    for process in processes:
    process.join()

    print('Done')


    If run


    Process 14608

    As you can see

    USER: As you can see

    It asks for input

    USER: It asks for input

    But backup_service() is not printing anything

    USER: But backup_service() is not printing anything



    Expected output


    Process 14608

    While I'm writing

    USER: While I'm writing

    BACKUP

    every second

    USER: every second

    backup_service() has to print 'BACKUP'
    USER: backup_service() has to print 'BACKUP'
    BACKUP



    Can anyone help?










    share|improve this question


























      0












      0








      0








      What I have:

      Web application that does some things, running on Bottle microframework, here is the Main



      def run_server():
      bottle.run(host=env.str('SRVRIP'), port=env.str('SRVRPORT'), debug=False)

      # Main
      if __name__ == '__main__':
      run_server()




      What I want to add:

      I want to split this app in 2 processes: first called main_service() which simply runs the server, second called backup_service() which has to take a periodic Db backup (once per hour)





      What I've tried

      First I've tried with the asyncio module, failing miserably (tried just for fun and to understand something new).
      Now I'm facing on multiprocessing (leaving aside threading cause I don't want GIL troubles), but I can't understand how to make it work, here is my code:



      import multiprocessing
      import time
      import os


      def main_service():
      print(f'Process os.getpid()')

      while(True):
      sentence=input()
      print(f'USER: sentence')


      def backup_service():
      print(f'Process os.getpgid()')

      while(True):
      print('BACKUP')
      time.sleep(1)


      if __name__ == "__main__":
      processes = []

      t = multiprocessing.Process(target=main_service())
      processes.append(t)
      t.start()

      t = multiprocessing.Process(target=backup_service())
      processes.append(t)
      t.start()

      for process in processes:
      process.join()

      print('Done')


      If run


      Process 14608

      As you can see

      USER: As you can see

      It asks for input

      USER: It asks for input

      But backup_service() is not printing anything

      USER: But backup_service() is not printing anything



      Expected output


      Process 14608

      While I'm writing

      USER: While I'm writing

      BACKUP

      every second

      USER: every second

      backup_service() has to print 'BACKUP'
      USER: backup_service() has to print 'BACKUP'
      BACKUP



      Can anyone help?










      share|improve this question
















      What I have:

      Web application that does some things, running on Bottle microframework, here is the Main



      def run_server():
      bottle.run(host=env.str('SRVRIP'), port=env.str('SRVRPORT'), debug=False)

      # Main
      if __name__ == '__main__':
      run_server()




      What I want to add:

      I want to split this app in 2 processes: first called main_service() which simply runs the server, second called backup_service() which has to take a periodic Db backup (once per hour)





      What I've tried

      First I've tried with the asyncio module, failing miserably (tried just for fun and to understand something new).
      Now I'm facing on multiprocessing (leaving aside threading cause I don't want GIL troubles), but I can't understand how to make it work, here is my code:



      import multiprocessing
      import time
      import os


      def main_service():
      print(f'Process os.getpid()')

      while(True):
      sentence=input()
      print(f'USER: sentence')


      def backup_service():
      print(f'Process os.getpgid()')

      while(True):
      print('BACKUP')
      time.sleep(1)


      if __name__ == "__main__":
      processes = []

      t = multiprocessing.Process(target=main_service())
      processes.append(t)
      t.start()

      t = multiprocessing.Process(target=backup_service())
      processes.append(t)
      t.start()

      for process in processes:
      process.join()

      print('Done')


      If run


      Process 14608

      As you can see

      USER: As you can see

      It asks for input

      USER: It asks for input

      But backup_service() is not printing anything

      USER: But backup_service() is not printing anything



      Expected output


      Process 14608

      While I'm writing

      USER: While I'm writing

      BACKUP

      every second

      USER: every second

      backup_service() has to print 'BACKUP'
      USER: backup_service() has to print 'BACKUP'
      BACKUP



      Can anyone help?







      python multiprocessing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago









      martineau

      68.8k1091185




      68.8k1091185










      asked 2 days ago









      HeleHele

      386




      386






















          2 Answers
          2






          active

          oldest

          votes


















          1














          You split your app in 2 processes but then you don't know what to do in the main process: why don't you simply run only one new process (for the backup) and you keep the web server loop running in the main process that started your program?



          Something like this should work (not tested):



          import multiprocessing
          import time
          import os

          def backup_service():
          print(f'Process os.getpgid()')

          while(True):
          print('BACKUP')
          time.sleep(1)


          if __name__ == "__main__":
          t = multiprocessing.Process(target=backup_service)
          t.daemon = True
          t.start()
          bottle.run(host=env.str('SRVRIP'), port=env.str('SRVRPORT'), debug=False)


          keep it simple...






          share|improve this answer

























          • Thanks, anyway doing this way my "main_service()" never print idk Why

            – Hele
            yesterday











          • sorry, there was a error in my code: you should pass only the function name without () to target Process.

            – Gabri Ele
            yesterday


















          1














          process.join() is never getting to the second process because process.join() only ends when the process that it's joining terminates. In your case, the process is an infinite loop so it never terminates and it never advances the loop to the next process.



          You could get around this and see some output from the other process by using the timeout option for join(). Alternatively, you could have your processes output to different files and tail those files to confirm that they're both running.






          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%2f55028034%2fmultiprocessing-help-launching-loops%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









            1














            You split your app in 2 processes but then you don't know what to do in the main process: why don't you simply run only one new process (for the backup) and you keep the web server loop running in the main process that started your program?



            Something like this should work (not tested):



            import multiprocessing
            import time
            import os

            def backup_service():
            print(f'Process os.getpgid()')

            while(True):
            print('BACKUP')
            time.sleep(1)


            if __name__ == "__main__":
            t = multiprocessing.Process(target=backup_service)
            t.daemon = True
            t.start()
            bottle.run(host=env.str('SRVRIP'), port=env.str('SRVRPORT'), debug=False)


            keep it simple...






            share|improve this answer

























            • Thanks, anyway doing this way my "main_service()" never print idk Why

              – Hele
              yesterday











            • sorry, there was a error in my code: you should pass only the function name without () to target Process.

              – Gabri Ele
              yesterday















            1














            You split your app in 2 processes but then you don't know what to do in the main process: why don't you simply run only one new process (for the backup) and you keep the web server loop running in the main process that started your program?



            Something like this should work (not tested):



            import multiprocessing
            import time
            import os

            def backup_service():
            print(f'Process os.getpgid()')

            while(True):
            print('BACKUP')
            time.sleep(1)


            if __name__ == "__main__":
            t = multiprocessing.Process(target=backup_service)
            t.daemon = True
            t.start()
            bottle.run(host=env.str('SRVRIP'), port=env.str('SRVRPORT'), debug=False)


            keep it simple...






            share|improve this answer

























            • Thanks, anyway doing this way my "main_service()" never print idk Why

              – Hele
              yesterday











            • sorry, there was a error in my code: you should pass only the function name without () to target Process.

              – Gabri Ele
              yesterday













            1












            1








            1







            You split your app in 2 processes but then you don't know what to do in the main process: why don't you simply run only one new process (for the backup) and you keep the web server loop running in the main process that started your program?



            Something like this should work (not tested):



            import multiprocessing
            import time
            import os

            def backup_service():
            print(f'Process os.getpgid()')

            while(True):
            print('BACKUP')
            time.sleep(1)


            if __name__ == "__main__":
            t = multiprocessing.Process(target=backup_service)
            t.daemon = True
            t.start()
            bottle.run(host=env.str('SRVRIP'), port=env.str('SRVRPORT'), debug=False)


            keep it simple...






            share|improve this answer















            You split your app in 2 processes but then you don't know what to do in the main process: why don't you simply run only one new process (for the backup) and you keep the web server loop running in the main process that started your program?



            Something like this should work (not tested):



            import multiprocessing
            import time
            import os

            def backup_service():
            print(f'Process os.getpgid()')

            while(True):
            print('BACKUP')
            time.sleep(1)


            if __name__ == "__main__":
            t = multiprocessing.Process(target=backup_service)
            t.daemon = True
            t.start()
            bottle.run(host=env.str('SRVRIP'), port=env.str('SRVRPORT'), debug=False)


            keep it simple...







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited yesterday

























            answered 2 days ago









            Gabri EleGabri Ele

            30328




            30328












            • Thanks, anyway doing this way my "main_service()" never print idk Why

              – Hele
              yesterday











            • sorry, there was a error in my code: you should pass only the function name without () to target Process.

              – Gabri Ele
              yesterday

















            • Thanks, anyway doing this way my "main_service()" never print idk Why

              – Hele
              yesterday











            • sorry, there was a error in my code: you should pass only the function name without () to target Process.

              – Gabri Ele
              yesterday
















            Thanks, anyway doing this way my "main_service()" never print idk Why

            – Hele
            yesterday





            Thanks, anyway doing this way my "main_service()" never print idk Why

            – Hele
            yesterday













            sorry, there was a error in my code: you should pass only the function name without () to target Process.

            – Gabri Ele
            yesterday





            sorry, there was a error in my code: you should pass only the function name without () to target Process.

            – Gabri Ele
            yesterday













            1














            process.join() is never getting to the second process because process.join() only ends when the process that it's joining terminates. In your case, the process is an infinite loop so it never terminates and it never advances the loop to the next process.



            You could get around this and see some output from the other process by using the timeout option for join(). Alternatively, you could have your processes output to different files and tail those files to confirm that they're both running.






            share|improve this answer



























              1














              process.join() is never getting to the second process because process.join() only ends when the process that it's joining terminates. In your case, the process is an infinite loop so it never terminates and it never advances the loop to the next process.



              You could get around this and see some output from the other process by using the timeout option for join(). Alternatively, you could have your processes output to different files and tail those files to confirm that they're both running.






              share|improve this answer

























                1












                1








                1







                process.join() is never getting to the second process because process.join() only ends when the process that it's joining terminates. In your case, the process is an infinite loop so it never terminates and it never advances the loop to the next process.



                You could get around this and see some output from the other process by using the timeout option for join(). Alternatively, you could have your processes output to different files and tail those files to confirm that they're both running.






                share|improve this answer













                process.join() is never getting to the second process because process.join() only ends when the process that it's joining terminates. In your case, the process is an infinite loop so it never terminates and it never advances the loop to the next process.



                You could get around this and see some output from the other process by using the timeout option for join(). Alternatively, you could have your processes output to different files and tail those files to confirm that they're both running.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 days ago









                JohnJohn

                56519




                56519



























                    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%2f55028034%2fmultiprocessing-help-launching-loops%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

                    Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                    2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

                    Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme