Home > Software engineering >  how to sleep just one function of the code in python?
how to sleep just one function of the code in python?

Time:12-05

i am writing a code in python and i just want one function to sleep not the whole code in time.sleep(). but i couldn't find a way. my code:

from time import sleep
a = int()
def calc(a,b):
    while True:
         a=a*b
         if a >> 12:
              sleep(12)
              #i just want this func to sleep here.
def print(msg):
    while True:
          msg = a
          print(msg)
          #i don't want this func to sleep

what should i do?

CodePudding user response:

Use asyncio

import asyncio

async def calc(a,b):
while True:
     a=a*b
     if a >> 12:
          await asyncio.sleep(12)

CodePudding user response:

I think You need to read a little about the function time.sleep. Here's what the documentation says about it:

Suspend execution of the calling thread for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.

The function pauses the program at the desired place, if you call it. It does not stop the other function.

  • Related