If a language does not support built-in concurrency, or if you are trying to tie two different languages together, one approach is "file polling". This is where files are used to communicate between each process/language. It is not meant for high-performance tasks, such as games, but can serve well for occasional needs.
Some useful tips:
- Write to a temporary file (one not being sought), and then rename it to the target name when it is complete. This prevents the reading of half-complete files.
- Usually have the reader delete a "consumed" message file, not the writer.
- Use a "sleep" or "wait" function in the middle of any wait loop. Try to avoid "wait" functions that hog CPU. Some OS's have system functions that tell the OS you are giving it permission to execute pending stuff from other apps, which is what you want if you are waiting.
- Or, if your system is slow enough, use cron on UNIX-like systems, and don't implement any loop.
- Don't wait forever. Have a max wait-loop threshold such that an error message or "retry" prompt appears.
- After doing it a few times and you get a feel, you can start to create utility functions/methods to do it for you.
-- top
This must be one SynchronizationPattern? which surely has been implemented thousands of times in the industry.
Advantages:
- Simple to understand
- Simple to implement
- Simple maintain
Disadvantages:
- Slow
- Works only well on large chunks of data
- May depend on file system semantics
- Very difficult to aggregate into larger processes
See also: TableOrientedSynchronization
CategoryConcurrency