0
from bin import pmGetter
from threading import Thread
import time

possibleRequests = ['test', 'test1']
inbox = []

inbox = pmGetter.getPms()


# time to do threading
def pmGetterLoop():
    while True:
        inbox = pmGetter.getPms()


def inboxReader():
    print('triggered')
    while True:
        tempInbox = []
        tempInbox = inbox.inboxMessage #inboxMesage remains filled?
        print(inbox.inboxMessage)
        i = 0
        while (i < len(tempInbox)):
            if (tempInbox[i] in possibleRequests):
                print('THIS IS WORKING')
            #print(i)
            i+=1
        time.sleep(2)

def commandBreaker(commandString):
    return commandString.split(',')


threadOne = Thread(target=pmGetterLoop)
threadTwo = Thread(target=inboxReader)

threadTwo.start()
threadOne.start()

#print(commandBreaker('this,is,a,test'))#this is what will be used to split commands

this is the main

import praw
import time


class getPms():
    r = praw.Reddit(user_agent="Test Bot By /u/**********")
    r.login(username='******************', password='*************')

    cache = []
    inboxMessage = []
    file = 'cache.txt'

    def __init__(self):

        self.cache = self.cacheRead(self.file)
        self.bot_run()
        self.cacheSave(self.file)
        time.sleep(2)
        #return self.inboxMessage

    def bot_run(self):
        print(self.inboxMessage, ' why?')
        self.inboxMessage = []
        inbox = self.r.get_inbox(limit=25)

        # print(r.get_friends())#this works
        for message in inbox:
            if message.id not in self.cache:
                #print(message.id)
                print(message.body)
                # print(message.subject)
                self.cache.append(message.id)
                self.inboxMessage.append(message.body)
                # else:
                # print("no messages")

    def cacheSave(self, file):
        with open(file, 'w') as f:
            for s in self.cache:
                f.write(s + '\n')

    def cacheRead(self, file):
        with open(file, 'r') as f:
            cache1 = [line.rstrip('\n') for line in f]
        return cache1

for some reason I get this output

[]  why?

test

this is a test

triggered

['test', 'this is a test']

THIS IS WORKING

[]  why? #this is from getPms.bot_run()

['test', 'this is a test']#this is from def inboxReader

THIS IS WORKING

where the [] why? indicates that the array has been emptied but ['test', this is a test'] indicates that the array has still been filled

I attempt to empty the array by doing self.inboxMessage = []

1 Answer 1

1

Here is a problem

inbox = pmGetter.getPms()

def pmGetterLoop():
    while True:
        inbox = pmGetter.getPms()

The inbox inside the pmGetterLoop function is not the same inbox that is outside the function. The are separate variables in separate scopes. If you want pmGetterLoop to be able to modify the global inbox variable, you need to tell it that it's using a global variable.

def pmGetterLoop():
    global inbox
    while True:
        inbox = pmGetter.getPms()
Sign up to request clarification or add additional context in comments.

2 Comments

thanks it kinda works now a few kinks here and there but I can work it out
Another problem is that getPms isn't entirely thread safe. You should use a Lock when you create it. Otherwise it's possible for thread 2 to access the inbox before getPms is finished instantiating

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.