2016-06-17 5 views
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 

を空にされていない、これは私がこの出力[]なぜ配列はpythonで

[] 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 

を得る何らかの理由でメイン

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 

のですか?ここで[]

答えて

1

=アレイが依然として

を満たされていることを示し、私はself.inboxMessageをすることによって、配列を空にしようとすると、配列が空になったことを示しているが、[「テスト」、これは "テストです]問題

inbox = pmGetter.getPms() 

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

pmGetterLoop関数内inboxは、関数の外にある同じinboxないからです。別々のスコープの別々の変数はです。 pmGetterLoopがグローバル変数inboxを変更できるようにするには、グローバル変数を使用していることを伝える必要があります。

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

それはちょっと、今ここにあるいくつかのねじれ動作しますが、私は –

+0

もう一つの問題は、getPmsが完全にスレッドセーフではないということです、それをうまくすることができ感謝します。あなたはそれを作成するときにロックを使用する必要があります。それ以外の場合、getPmsのインスタンス化が完了する前にスレッド2が受信ボックスにアクセスする可能性があります –