optimize operation needs a lock on table, so you cant optimize tables in real time application.
You can setup a slave and execute optimization on slave and promote optimized slave as master.
We were experiencing an issue with one of our tables where every update resulted in a lock wait timeout. One option we tried in order to resolve this was to optimize the table, however this also resulted in a lock wait time out despite no other operations being made on the table at the same time.
My next attempts where to flush and check the table, which seemed fine, and may have resolved the issue but we went straight on to create a copy of the table, via dump and import and rename the tables effectively replacing the old one with the new one.
This seems to have resolved the issue for the time being and updates are being made in a timely fashion again.