0

I want to run

import time
number1 = 1
while True:
    number1 = int(number1) + 1
    time.sleep(3)

And

import time
number2 = 1
while True:
    number2 = int(number2) + 1
    time.sleep(20)

At the same time in python, how would I go about doing so? I'm still a beginner, please explain it in simpler terms if you can.

Sub-question: There is little of actual code and more of waiting around, would it be better to use multithreading or multiprocessing?

Sub-question2: Can I run more processes than the number of cores my cpu has in multiprocessing?

  • You could use `multiprocessing` or `asyncio` or another concurrent library, or just in the loop,increment`number1` then sleep 3 seconds increment`number2` then sleep 17 seconds. – Jab Nov 22 '21 at 18:26
  • In the last solution you gave, number1 won't be able to update every 3 secs. –  Nov 22 '21 at 18:31
  • Is there any specific reason why you want to run them at the same time and not just one after another? – CAP Nov 22 '21 at 18:49
  • Yes, I need to request from a webpage for each of them at different times. If it was one after the other, I would have to wait until the first one finished. –  Nov 22 '21 at 18:53
  • Correct me if I'm not understanding correctly, but you are making a request for number1 and then later making a request for number2? I'm mainly asking because I would strongly recommend not using multiprocessing, but don't know what is best without context of what this is needed/used for. – CAP Nov 22 '21 at 18:56
  • Sorry for now giving context, I'll give a bit of context. I have to request for the data of a few messages to see if they trigger any key words, and I need this to run at the same time in many places. If I run it one after the other, I won't be able to respond as quickly as I would be able to if it were requesting for the data of the messages every 2 seconds or so. What are the drawbacks to multiprocessing? My code is really simple. If it has too many drawbacks, I can just use a bat file to run multiple instances of the file, would this be a better approach? I'm sorry if I sound a bit rude. –  Nov 22 '21 at 19:03
  • I'm not meaning to be rude, but would it be better to run a batch file that runs multiple instances of my code or would it be better to use multiprocessing? Right now my code only uses 2MB memory to run, it's running one after the other so it's not as responsive as I would like it to be. –  Nov 22 '21 at 19:05

2 Answers2

0

You can use multiprocessing.

from multiprocessing import Process
import time
def loop_3():
    number1 = 1
    while True:
        number1 = int(number1) + 1
        time.sleep(3)
        print(number1)

def loop_20():
    number2 = 1
    while True:
        number2 = int(number2) + 1
        time.sleep(20)
        print(number2)


Process(target=loop_3).start()
Process(target=loop_20).start()
Alex
  • 281
  • 2
  • 7
0

I would recommend using concurrency. For your purpose, it will look like everything is happening "at the same time" (here is a more in depth explanation of the two: What is the difference between concurrency, parallelism and asynchronous methods?).

You could implement concurrency by doing something like the following:

def f(num, wait):
  time.sleep(wait)
  return int(num) + 1
  
numbers = [1, 2]
test = [f(num, 3) for num in numbers]

Here is another example of concurrency I wrote up in typescript that you could play around with: ts example

Note: I would warn against using multithreading / parallelism, especially if you're new to python. It can cause a lot of issues, is difficult to debug / understand, and can often cause a hit to performance rather than a gain if not implemented correctly.

Note 2: If you feel comfortable creating multiple instances and running them at the same time (as you described with bat files) you could go ahead and do that, but this makes your project difficult to share.

CAP
  • 120
  • 1
  • 9
  • Thank you! This is my favourite solution so far! I think concurrency is exactly what I need, my code takes time not because of what it has to process, but because it has to wait a lot. This is perfect! –  Nov 23 '21 at 06:59
  • Glad to hear, good luck with implementation. FYI you may need some async / awaits. – CAP Nov 23 '21 at 14:28
  • Thank you for extracting the whole problem out of me, I'm not very good at explaining what I want to explain. –  Nov 23 '21 at 17:26