Jul-27-2022, 10:17 PM
(This post was last modified: Jul-28-2022, 09:25 AM by Larz60+.
Edit Reason: fixed python tags please use python, not quotes
)
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]](https://i.imgur.com/rh49nLZ.png)
how make this work? my actual code is
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]](https://i.imgur.com/rh49nLZ.png)
how make this work? my actual code is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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() |