Python Forum
Why thread and queue only process the last row - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why thread and queue only process the last row (/thread-17300.html)



Why thread and queue only process the last row - kinojom - Apr-05-2019

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.