Question

Im trying to write a chat logic, so here what I want to do

def chatlogic():
    talk=True
    while talk:
        if __name__ == '__main__':
            Thread(target = fluse).start()
        message = raw_input('enter a message: ')
        if not message: #Ending Conversation if empty message was sent
            talk=False          
        conv_file.write('[%s]%s: %s\n' %(msgtime,user,message))
        #conv_file.flush()
        return 'Conversation Ended'

def fluse():
    while True:
        time.sleep(1)
        conv_file.fluse()

the file must update every second, nomatter what you are doing. What am i doing wrong? note: I have never used multitasking before

Was it helpful?

Solution

There are many problems in this code, but the one you seem to be asking about is here:

while talk:
    if __name__ == '__main__':
        Thread(target = fluse).start()

This means that each time through the loop—that is, once per message—you're going to fire off a new fluse thread. And that thread loops forever, calling conv_file.fluse() every second until the end of time.

So, if you type messages at 0.3, 2.7, and 5.1 seconds after app startup, you're going to get a fluse at 5.3 seconds, another at 5.7, another at 6.1, and so on.

If you want this to only happen once/second, just start a single thread, instead of starting a new one each time through the loop. For example:

if __name__ == '__main__':
    Thread(target = fluse).start()
while talk:
    # etc.

If, instead, you want it to happen a second after each write, but not every second again after that, just take the loop out of the function:

def fluse():
    time.sleep(1)
    conv_file.fluse()

Although in this case, threading.Timer is an easier way to do the same thing.


Anyway, even with this fix, as I said, there are a number of other problems:

  • You're calling a file method that doesn't exist—presumably you meant flush instead of fluse?
  • The file doesn't exist either. Maybe you meant it to be a global, created outside the functions? Or an argument to them?
  • You're trying to loop forever until an empty message, but you call return each time through the loop, which means you're only going to loop once.
  • There's no way to exit your program, because you fire off a non-daemon background thread that runs forever.
  • You never call the chatlogic function anyway, so the program just exits immediately without doing any work.

If I fix all of the other errors in your program, and add something that shows me whether the fluse thread is doing its job, it does something… and maybe you can tell us whether it's the thing you wanted.

from threading import Thread
import time

def chatlogic(conv_file):
    user = 'Nobody'
    t = Thread(target=flusher, args=[conv_file])
    t.daemon=True
    t.start()
    while True:
        message = raw_input('enter a message: ')
        if not message: #Ending Conversation if empty message was sent
            break
        msgtime = time.asctime()
        conv_file.write('[%s]%s: %s\n' %(msgtime,user,message))
    return 'Conversation Ended'

def flusher(conv_file):
    while True:
        time.sleep(1)
        print 'Flushing the file now'
        conv_file.flush()

if __name__ == '__main__':
    conv_file = open('convfile.txt', 'w')
    chatlogic(conv_file)

This is still a strange design. The file is being flushed once/second even if you didn't write anything, and that there's no guarantee it actually gets flushed at the end, and so on. But this is what it sounds like you were attempting to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top