luckydonald / pytg

Python package that wraps around Telegram messenger CLI. Send and receive messages, and more.
MIT License
369 stars 76 forks source link

Pytg module does not handle signal while waiting for _new_messages.acquire() #42

Open naveenvhegde opened 9 years ago

naveenvhegde commented 9 years ago

I think it should be conditional wait than semaphore.

If signal is issued, the program will come out and handle the signal only after another message is received..

luckydonald commented 9 years ago

I don't think I understand your issue, sorry.

naveenvhegde commented 9 years ago

Hi,

i debugged the receiver code a bit, what i understand from that code is.

  1. Receiver thread receives a ANSWER and push into the queue and release a semaphone
  2. Main thread will yield from the queue and wait for the another message to arrive (i.e semaphore.acquire()).

one main problem i noticed with this, while main thread waiting for next message (blocking sempare.acquire() call), if i issue a sigint, main thread will not recieve that signal because it is waiting on the blocking call. program will not handle ^C (Ctrl + C). i have to issue kill -9 to stop the process.

the alternative solution will be, instead of semaphore, you can use threading.Condition along with the lock.

  1. Reciver thread calls condition.notifyall when data arrives.
  2. Main thread can do conditional wait (i.e condition.wait(4)), so that it wakes up every 4 sec or so. by doing this main thread can easily handle the signals/interrupts.

Please let me know if you think otherwise.

luckydonald commented 9 years ago

I have not looked into threading.Condition and condition.wait() but this sounds like busy waiting for me, more like a polling solution. Which might introduce higher CPU usage again. (Thats what the whole queue stuff is for in the first place)

I am currently a bit busy so I can't test anything, and don't have the time to read about that in the docs. Maybe next week I might get time to look into that.