I need to make a multithreading program (python)

Posted by Andreawu98 on Stack Overflow See other posts from Stack Overflow or by Andreawu98
Published on 2014-06-09T09:43:00Z Indexed on 2014/06/09 15:26 UTC
Read the original article Hit count: 182

Filed under:
|
import multiprocessing
import time
from itertools import product
out_file = open("test.txt", 'w')
P = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p','q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',]
N = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
M = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
c = int(input("Insert the number of digits you want: "))  
n = int(input("If you need number press 1: "))               
m = int(input("If you need upper letters press 1: "))            
i = []
if n == 1:
    P = P + N
if m == 1:
    P = P + M
then = time.time()
def worker():
    for i in product(P, repeat=c):      #check every possibilities
        k = ''
        for z in range(0, c):           #
            k = k + str(i[z])           #   print each possibility in a txt without parentesis or comma
    out_file.write( k + '\n')           #
    out_file.close()
    now = time.time()
    diff = str(now - then)              # To see how long does it take
    print(diff)
worker()
time.sleep(10)                  # just to check console

The code check every single possibility and print it out in a test.txt file. It works but I really can't understand how can I speed it up. I saw it use 1 core out of my quad core CPU so I thought Multi-threading might work even though I don't know how. Please help me. Sorry for my English, I am from Italy.

© Stack Overflow or respective owner

Related posts about python

Related posts about multithreading