Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multithreading
#1
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()
Reply
#2
You may find this to be a good guide to multithreading; I did.

https://www.pythontutorial.net/python-co...d-threads/
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
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.
Gribouillis likes this post
Reply
#4
fixed
Reply
#5
Hanyx Wrote:fixed
Please share results, so others may benefit.
rob101 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Problems with variables in multithreading Wombaz 2 1,343 Mar-08-2022, 03:32 PM
Last Post: Wombaz
  Multithreading question amadeok 0 1,788 Oct-17-2020, 12:54 PM
Last Post: amadeok
  How can i add multithreading in this example WoodyWoodpecker1 3 2,519 Aug-11-2020, 05:30 PM
Last Post: deanhystad
  matplotlib multithreading catosp 0 2,962 Jul-03-2020, 09:33 AM
Last Post: catosp
  Multithreading dynamically syncronism Rodrigo 0 1,544 Nov-08-2019, 02:33 AM
Last Post: Rodrigo
  Locks in Multithreading Chuonon 0 1,853 Oct-03-2019, 04:16 PM
Last Post: Chuonon
  multithreading issue with output mr_byte31 4 3,217 Sep-11-2019, 12:04 PM
Last Post: stullis
  Multithreading alternative MartinV279 1 2,804 Aug-01-2019, 11:41 PM
Last Post: scidam
  using locks in multithreading in python3 srm 2 3,681 Jul-13-2019, 11:35 AM
Last Post: noisefloor
  Multithreading in a loop valtih 3 16,414 Aug-03-2017, 08:20 PM
Last Post: valtih

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020