Python3 -> Is there a funcion like millis() of arduino?

Hello.
I use python3 and I want to do something for 3 seconds(for example blink a led) , but I don’t want to use sleep() function.
Is there a function like millis() of arduino to use and if yes how can I use it (any help/link)??
I want to use a millis() function ,because I want to control many things at any time.
(I also asked in raspberry pi forum)
Thanks!!!

(deleted)

I also asked in raspberry pi forum as I said
I ask here, because maybe someone knows the answer

import time
time.monotonic(

Using a continuously running loop that checks a high-resolution timer is NOT a good way to program most multitasking operating systems.

Can you be more specific?

Thank you!!

With a real-time multitasking OS being sat in a constant loop, like you do with an Arduino sketch, is bad.
It ties up a CPU core doing absolutely nothing other than checking if a particular amount of time has passed.
The correct programming paradigm in a RTOS is to wait, usually on completion of some other thread, IO operation, etc
To do more than one thing at a time you simply launch another thread/task.

you can define a (lambda) function that will get you there

 import time
millis = lambda: int(round(time.time() * 1000))

When you want to know what's the value of millis, then you just callmillis()

[color=purple]
[b]$> python3[/b]
Python 3.7.3 (default, Mar  6 2020, 22:34:30) 
[Clang 11.0.3 (clang-1103.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> millis = lambda: int(round(time.time() * 1000))
>>> 
>>> millis()
1591821773211
>>> 
>>> 
>>> millis()
1591821780859
>>> 
>>> 
>>> millis()
1591821785163
>>> 
>>> 
>>> ^D
[/color]

but sleeping is a better idea unless you have other stuff to do, you'll give back CPU time to the rest of your system.

How can I check if (for example) 2 seconds have passed?

well if millis() has increased by more than 2000...

you record millis() in a variable startChrono
you check if millis() minus startChrono is larger than 2000

just the way you would do it on an Arduino.... read the millis tutorials...

arduinick:
How can I check if (for example) 2 seconds have passed?

You can use the function from Post #2 in Python essentially as you would millis() in an Arduino sketch. It may not be the optimum approach from the perspective of other process running on the computer, but it will generally work.

The criticism of using this sort of program structure is similar to the argument against using delay() on Arduino. One can certainly use it and it can be useful in certain contexts, but it blocks the processor (or processor core on a multicore CPU) from doing anything else. This may or may not matter depending upon the application.

On a processor without a multitasking operating system, like Arduino, one must explicitly program multitasking as in the BlinkWithoutDelay example. The same program structure under a multitasking OS, essentially defeats the OS's ability to do scheduling and make use of excess CPU cycles on other processes.

you can define a (lambda) function that will get you there

 import time

millis = lambda: int(round(time.time() * 1000))

I'll bite: why would you use lambda instead of a traditional python function:

def millis():
  return round(time.time() * 1000)

Why not just spawn a Python thread and have the thread loop and sleep, the main program can keep on running.

Idahowalker:
Why not just spawn a Python thread and have the thread loop and sleep, the main program can keep on running.

Because it’s a classic X-Y problem…

The OP doesn’t know how to do that either (or perhaps that it is even possible) so instead they are asking about some other coding problem they have engineered to workaround that limitation in their knowledge of how to solve the problem properly in the first place.

What the OP really needs to do is find the Python / Pi forum and ask about what they really want to do there.

westfw:
I'll bite: why would you use lambda instead of a traditional python function:

Well both are possible, I mentioned function in my post but went for lambda as I tend to favor lambdas when doing interactive Python and they match the single expression rule: it’s faster to type as you don’t need the multi line crap and precise formatting python expects. I always hated the fact that indentation (invisible characters) drives structure.

Also there is a slight memory win as lambdas do not carry documentation if I remember well.

But truth to be told: laziness and personal preference is probably the most honest answer :slight_smile:

westfw:
I'll bite: why would you use lambda instead of a traditional python function:

def millis():

return round(time.time() * 1000)

May I have another bite ....

Why not just use time.time directly and stick with standard Python ?

...R

Robin2:
May I have another bite ....
Why not just use time.time directly and stick with standard Python ?

Yeah I was just answering the specific question for a millis() function and time.time() could be another way of doing it. (but a function is also "standard Python")

it's not even fool proof as time() returns the time as a floating point number expressed in seconds since the epoch and even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second... so the *1000 stuff and taking the integral part is not guaranteed to work everywhere and you'll have second resolution, not millis...

There's lot to say about that ask, and I agree there is a XY issue there.