Python Forum

Full Version: Why thread and queue only process the last row
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
CSv
date,ref_num,desc,debit,credit,payee_id,code,name

    2019-01-31,L00001,john,30.00,,150,500,johnkino
    2019-01-31,L00001,john,,30.00,150,600,johnkino
    2019-01-30,L00002,john,30.00,,150,500,johnkino
    2019-01-30,L00002,john,,30.00,150,600,johnkino
import csv
from itertools import groupby
import threading
from queue import Queue

def postRequest():
    print('thread starting')
    while True:
        item_to_process = q.get()
        print(item_to_process)
        q.task_done()

q = Queue()

with open("test.csv", "r") as csv_ledger:
    r = csv.DictReader(csv_ledger)
    data = [ dict(d) for d in r ]
    groups = {}

    for k, g in groupby(data, lambda r: (r[ 'ref_num' ])):
        items = [ ]
        for i in g:
            chart_of_account = {k: v for k, v in i.items() if k in [ 'name', 'code' ]}
            item = {k: v for k, v in i.items() if k in [ 'debit', 'credit', 'desc', 'payee_id' ]}
            item.update({'chart_of_account': chart_of_account})
            items.append(item)
        groups.update({
            "date": i[ 'date' ],
            "desc": i[ 'desc' ],
            "ref_num": k,
            "items": items
        })
        print(groups) <--- correct structure
        q.put(groups)


for i in range(1):
    t = threading.Thread(target=postRequest)
    t.start()

print('Main thread waiting')
q.join()
print('Done.')
Result
2019-01-30,L00002,john,,30.00,150,600,johnkino
2019-01-30,L00002,john,,30.00,150,600,johnkino
2019-01-30,L00002,john,,30.00,150,600,johnkino
2019-01-30,L00002,john,,30.00,150,600,johnkino
The data of groups is the complete and correct data structure which is loop every row but the result always loops for the last row only after passing to queue for processing.