0

I'm creating an instant messenger program for my school's common drive. I have everything working except for on small detail. In the code below it checks for a new message from a friend and prints the last message they sent. If there are no messages it says so. The problem is when it moves to the next step of the code it waits for the user to put in an input. Until you give an input it won't let you receive any more messages because the program stops reading and searching the while loop and gets caught on the input statement. I want to know if there is anyway to make an input statement optional. To say that it doesn't require an input but if there is an input it will send it and do it's thing. I just can't seem to figure out a way to make the input statement optional. Any ideas or working code would be greatly appreciated. If you need the entire code I don't have a problem with sending it to you or posting it. This is the only bit of code that should really matter for this problem though.

        LastMessage = ""
        while Message:

            Path = "Message"+SendTo+"-"+UserName+".txt"

            if path.isfile(Path):

                LoadMessage = open(Path, "rb")
                NewMessage = pickle.load(LoadMessage)
                LoadMessage.close()

            else:
                NewMessage = "Sorry, No messages found"   

            if LastMessage != NewMessage:

                LastMessage = NewMessage
                print(NewMessage)
                print("")

            SendMessage = raw_input()  #--- This is where it gets caught up! ---

            Save = open("Message"+UserName+"-"+SendTo+".txt", "wb")
            pickle.dump(SendMessage, Save)
            Save.close()
  • If I understand correctly, you want to be able to print the new messages and recieve input from the user at the same time? Why not use Threads? – Lazybeem Dec 12 '13 at 23:34

2 Answers2

0

You have two main options as I see it:

  1. Simultaneous input and checking (various options, search for e.g. threading or multiprocessing from the standard library); or
  2. Input with timeout and loop (see e.g. How to set time limit on raw_input).
Community
  • 1
  • 1
jonrsharpe
  • 99,167
  • 19
  • 183
  • 334
0

So it sounds like you want to do two separate things at the same time - look for input from a user and poll for new messages from other users. Jonrsharpe gives threading as his first option to solve this and I agree its the most straightforward. What you need to do is something like this:

    import threading

    class InputMessageThread(threading.Thread):
        def run(self):
            SendMessage = raw_input()  # This thread will hang here for input but thats 
                                       # OK as original thread will keep going
            Save = open("Message"+UserName+"-"+SendTo+".txt", "wb")
            pickle.dump(SendMessage, Save)
            Save.close()

    inputthread = InputMessageThread()
    inputthread.start()

    # rest of your code here

While you are at it though you might want to look at some other issues. For example if I understand what you are trying to do correctly you are going to have a file containing a message from a source user to a destination user. But if the source user sends a second message before this file gets processed then the first message will be overwritten. In practice you may never see this but some sort of handshaking to make sure the message has actually been sent before you allow the next to be written would be a more robust approach.

Tommy
  • 627
  • 5
  • 8
  • I plan on adding some more features to take care of that problem. I figured that would be a problem too. I thought I'd create a list for each person to person. The list would be a list of messages. It'll read how many items are in the list and add 1 when creating a new message. I figured that would be the best way to take care of that problem. Anyways, thanks for the code, I'm looking forward to seeing if it works like I'm hoping. ^_^ – user3097485 Dec 18 '13 at 19:01