Is it possible to execute two functions at EXACTLY the same time

Matt

I'm wanting to take photos from 2 different cameras at exactly the same time (or as close as possible).

If I use multithreading or multiprocessing, it still runs the threads/processes consecutively.. For instance if I start the following processes:

Take_photo_1.start()
Take_photo_2.start()

While those processes would run in parallel, the commands to start the processes are still executed sequentially. Is there any way to execute both those processes at exactly the same time?

abarnert

There's no way to make this exact even if you're writing directly in machine code. Even if you have all the threads wait on a kernel barrier, that wait can take different times on different cores, and there are opcodes to process between the barrier wait and the camera get that have to get fetched and run on a system where the caches may be in different states, and there's nothing stopping the OS from stealing the CPU from one of the threads to run some completely unrelated code, and the I/O to the camera (even if it isn't serialized, which it may be) probably isn't a guaranteed static time, and so on.

When you throw an interpreted language on top of it (especially one with a GIL, like Python, which means the bytecodes between the barrier wait and the camera get can't be run in parallel)… well, you're not really changing anything; "impossible * 7" is still "impossible". But you are making it even more obvious.

Fortunately, very few real-life problems have a true hard real-time requirement like that. Instead, you have a requirement like "99.9% of the time, all camera gets should happen within +/-4ms of the desired exact 30fps". Or, maybe, "90% of the time it's within +/-1ms, 99.9% of the time it's within +/-4ms, 99.999% of the time it's within +/-20ms, as long as you don't do anything stupid like change the wall-power state of the laptop while running the code".

Or… well, only you know why you wanted "exact", and can figure out what the actual requirements are that would satisfy you.

And for that case, often the simplest thing to do is write the code the obvious way, stress test the hell out of it, see if it meets your requirements, and figure out how to optimize things only if it doesn't.

So, your existing code may well be fine.

If not, adding a shared barrier = threading.Barrier() and doing a barrier.wait() right before the camera.get() may be all you need.

You may need to add logic to detect timer lag and re-synchronize (which you might do independently in each thread, or have whichever thread gets there first compute it and just make everyone else wait at the barrier).

You may need to rewrite the core loop in C. Or dump whichever OS you're using for one with better real-time guarantees like QNX. Or throw out the OS entirely so there's no scheduler to get in the way. Or throw out the complex superscalar CPUs and implement the whole thing as a hardware state machine. Or…

But, assuming you have reasonable requirements in the first place, you usually don't have to go very far.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

2 functions executed at the same time. Is it possible?

分類Dev

2 functions executed at the same time. Is it possible?

分類Dev

Is it possible to have two or more active exceptions at the same time?

分類Dev

In Singleton Pattern,what happen when a two or more threads execute same time?

分類Dev

Execute stored functions at run-time

分類Dev

Activate Two Cameras At The Same Time

分類Dev

Python How to find a word but not exactly the same between two list?

分類Dev

is it possible for JWT to generate a same token, two times?

分類Dev

Javascript execute two functions addEventlistener and delay one of them

分類Dev

How to execute two functions one by one in python and get the desired output

分類Dev

Is it possible to execute two react-native contexts in different threads?

分類Dev

Running two tasks at the same time in Java

分類Dev

Java - Executing two corrosponding for loops at same time

分類Dev

Trying to rank two arrays at the same time

分類Dev

How to spin two components at a same time in uipickerview

分類Dev

Using two different networks at the same time

分類Dev

Replacing two patterns in a text files at the same time

分類Dev

Take input from user and execute the python script at the same time

分類Dev

cron jobs set for different times, but execute at the same time.

分類Dev

Kotlin: Possible to modify functions during compile time through metaprogramming?

分類Dev

Is Firebase Firestore real-time updates using Cloud functions possible?

分類Dev

Exactly same folder and drive?

分類Dev

In PHP what is the most efficient way to create two array items which have exactly the same value?

分類Dev

How to join two columns using 'on' statement if values in each column are not exactly the same?

分類Dev

Need help in connecting two tables without exactly the same info in the joined tables

分類Dev

How to use Sub and GetAtt functions at the same time in CloudFormation template?

分類Dev

Javascript - Functions won´t run the same way after the first time

分類Dev

Haskell : Two different functions using the same where clauses

分類Dev

Is is possible to update a version of my app with price change at the same time

Related 関連記事

  1. 1

    2 functions executed at the same time. Is it possible?

  2. 2

    2 functions executed at the same time. Is it possible?

  3. 3

    Is it possible to have two or more active exceptions at the same time?

  4. 4

    In Singleton Pattern,what happen when a two or more threads execute same time?

  5. 5

    Execute stored functions at run-time

  6. 6

    Activate Two Cameras At The Same Time

  7. 7

    Python How to find a word but not exactly the same between two list?

  8. 8

    is it possible for JWT to generate a same token, two times?

  9. 9

    Javascript execute two functions addEventlistener and delay one of them

  10. 10

    How to execute two functions one by one in python and get the desired output

  11. 11

    Is it possible to execute two react-native contexts in different threads?

  12. 12

    Running two tasks at the same time in Java

  13. 13

    Java - Executing two corrosponding for loops at same time

  14. 14

    Trying to rank two arrays at the same time

  15. 15

    How to spin two components at a same time in uipickerview

  16. 16

    Using two different networks at the same time

  17. 17

    Replacing two patterns in a text files at the same time

  18. 18

    Take input from user and execute the python script at the same time

  19. 19

    cron jobs set for different times, but execute at the same time.

  20. 20

    Kotlin: Possible to modify functions during compile time through metaprogramming?

  21. 21

    Is Firebase Firestore real-time updates using Cloud functions possible?

  22. 22

    Exactly same folder and drive?

  23. 23

    In PHP what is the most efficient way to create two array items which have exactly the same value?

  24. 24

    How to join two columns using 'on' statement if values in each column are not exactly the same?

  25. 25

    Need help in connecting two tables without exactly the same info in the joined tables

  26. 26

    How to use Sub and GetAtt functions at the same time in CloudFormation template?

  27. 27

    Javascript - Functions won´t run the same way after the first time

  28. 28

    Haskell : Two different functions using the same where clauses

  29. 29

    Is is possible to update a version of my app with price change at the same time

ホットタグ

アーカイブ