0

I am working on pulling weather data from a number of sources online, here is some of my code to get this data.

import wunderground as wg
import weatherScraper as wp

def main():
    wg.main()
    ws.main()

if (__name__ == "__main__"):
    main()

Both of the main functions contain sleep functions. I was wondering if it would be possible to run both simultaneously? Currently it runs one, has the sleep function activate, and such.

Johannes
  • 3,300
  • 2
  • 20
  • 35
Professor_Joykill
  • 919
  • 3
  • 8
  • 20
  • 4
    You are looking for [threading](https://docs.python.org/2/library/threading.html). – Arount Jun 30 '17 at 14:06
  • You need to run one (or both) on separate threads in the background. Note, this has the potential to add a *lot* of complexity if you aren't ready for it, so be sure to read up thoroughly on Multithreading. – Carcigenicate Jun 30 '17 at 14:15

2 Answers2

2

There are many ways of doing this. If you don't want to make one script for each function, you may use the multiprocessing module and use the Process object. There is a simple example here, of course without using a global variable. Also you may check the module documentation here

I think your code would be like this:

import wunderground as wg
import weatherScraper as wp
from multiprocessing import Process

if (__name__ == "__main__"):
    p1 = Process(target = wg.main())
    p1.start()
    p2 = Process(target = ws.main())
    p2.start()

Or you may use another parallel modules like the ones listed here on the Symmetric Multiprocessing section.

Cheers.

RZRKAL
  • 417
  • 4
  • 12
1

I would use the Python multiprocessing module.

Maybe something like:

import wunderground as wg
import weatherScraper as wp
from multiprocessing import Process

f1 = wg.main
f2 = ws.main

p1 = Process(target=f1)
p2 = Process(target=f2)
p1.start()
p2.start()

See Python: Executing multiple functions simultaneously

aquil.abdullah
  • 3,059
  • 3
  • 21
  • 40