0

I'm trying to call the same function with different parameters in parallel using multiprocessing module.

Here is example of my code:

from django.apps import AppConfig

class CurrencyRatesConfig(AppConfig):
    name = 'currency_rates'

    def ready(self):
        import time
        from multiprocessing import Process

        import currency_rates.ws_get_rates

        p1 = Process(currency_rates.ws_get_rates.get_rates('1m', 'tBTCUSD', 'BTC2USD'))
        p1.start()
        p2 = Process(currency_rates.ws_get_rates.get_rates('1m', 'tIOTUSD', 'IOT2USD'))
        p2.start()

        p1.join()
        p2.join()

But it doesn't work. I see just p1's execution result.

NOTE: get_rates() uses websocket module.

Could somebody describe what is wrong?

Thanks.

Raman
  • 107
  • 1
  • 3
  • 12
  • I believe p1.join() will block until p1 returns. Then p2 would resume. – Ron Norris Oct 18 '17 at 12:23
  • That doesn't block the other processes, only this one – jonatan Oct 18 '17 at 12:23
  • have a look at this answer https://stackoverflow.com/a/19138327/5476782 if you don't need join(), remove it from your code, it blocks the thread in execution – Evhz Oct 18 '17 at 12:25
  • @Karlos there's no code after the joins, nothing is being blocked from running. There was an answer here a minute ago that seems plausible about the work carried out by get_rates which is called in the current process and is therefore not parallel – jonatan Oct 18 '17 at 12:28
  • the code after doesn't matter. With join you block the thread which lauched the p2, so no answer from p2. Have a look at https://pymotw.com/2/multiprocessing/basics.html – Evhz Oct 18 '17 at 12:30
  • @Karlos , even without join() p2 doesn't start – Raman Oct 18 '17 at 12:34

1 Answers1

0

The problem is with the Process arguments. Modify it as follows:

This demo does work. Not sure why the specific issue is not resolved for OP.

from multiprocessing import Process
import time
import sys

def outputString(thestring):
    for i in range(3):
        print(thestring)
        time.sleep(2)
    return

def main():
    p1 = Process(target=outputString, args=('P1 is processing',))
    p1.start()
    p2 = Process(target=outputString, args=('P2 is processing',))
    p2.start()

if __name__ == "__main__":
    main()

OP original code (modified)

from django.apps import AppConfig

class CurrencyRatesConfig(AppConfig):
    name = 'currency_rates'

    def ready(self):
        import time
        from multiprocessing import Process

        import currency_rates.ws_get_rates

        p1 = Process(target=currency_rates.ws_get_rates.get_rates, args=('1m', 'tBTCUSD', 'BTC2USD'))
        p1.start()
        p2 = Process(target=currency_rates.ws_get_rates.get_rates, args=('1m', 'tIOTUSD', 'IOT2USD'))
        p2.start()
Ron Norris
  • 2,642
  • 1
  • 9
  • 13
  • Doesn't work. Probably because I'm using `websocket` module in `get_rates()` – Raman Oct 18 '17 at 12:40
  • Meaning it still blocks? Not sure how unless all resources on the machine are consumed by `get_rates()` because they run in completely different process space. But you can't have the join() in their, or it will not work in parallel. – Ron Norris Oct 18 '17 at 12:50