Python Forum

Full Version: multithreading
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
multithreading in python

2 files

messages.txt with indefinite number of lines keys.txt with indefinite number of lines

example:

keys.txt have 4 items

messages.txt have 12 items

my program will create 1 thread for each item on keys.txt(4 threads) calling a function with 2 items from messages.txt and so on until the message.txt file ends.

example2:

keys.txt have 2 items

messages.txt have 6 items

keys.txt

key1 key2

messages.txt

message1 message2 message3 message4 message5 message6

create 2 threads each time

[Image: rh49nLZ.png]

how make this work? my actual code is

from concurrent.futures import ThreadPoolExecutor

MENSAGENS = 'mensagens.txt'
CHAVES = 'keys.txt'
WORKERS = 2

def encode(mensagem, key):
    print(mensagem+" "+key)
    
def main():
    with open(MENSAGENS) as mensagens:
            msg = [line.rstrip() for line in mensagens]
            with ThreadPoolExecutor(WORKERS) as executor:
                    executor.map(encode, msg)
                    executor.shutdown()

if __name__ == '__main__':
    main()
You may find this to be a good guide to multithreading; I did.

https://www.pythontutorial.net/python-co...d-threads/
I would suggest multiprocessing https://pymotw.com/3/multiprocessing/index.html to take advantage of multi-cores, and a multiprocessing Pool https://ao.ms/multiprocessing-pools-in-python/ if you could have more processes than cores.
fixed
Hanyx Wrote:fixed
Please share results, so others may benefit.