![]() |
|
Multi-threading Help? - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Python (https://sinister.li/Forum-Python) +--- Thread: Multi-threading Help? (/Thread-Multi-threading-Help) |
Multi-threading Help? - Eclipse - 01-24-2015 Code: import string, random
from random import randint
def id_generator(size=6, chars=string.ascii_uppercase):
return ''.join(random.choice(chars) for _ in range(size))
def random_no(n):
range_start = 10**(n-1)
range_end = (10**n)-1
return randint(range_start, range_end)
f = open('list.txt', 'r+')
def gen():
s = str(str(id_generator()) + str(random_no(2)) + 'c')
if not s in f.readlines():
print(s)
f.write(s + '\n')
while True:
gen()How would I implement multi-threading in the example above? Say, two threads running simultaneously. RE: Multi-threading Help? - blackhatcat - 01-24-2015 Code: import string, random
from random import randint
from threading import Thread
def id_generator(size=6, chars=string.ascii_uppercase):
return ''.join(random.choice(chars) for _ in range(size))
def random_no(n):
range_start = 10**(n-1)
range_end = (10**n)-1
return randint(range_start, range_end)
def gen():
s = str(str(id_generator()) + str(random_no(2)) + 'c')
print(s)
with open('list.txt', 'a+') as f:
if not s in f.readlines():
f.write(s + '\n')
f.close()
def worker(imgaydad):
gen()
while True:
thread1=Thread(target=worker,args=("thread 1",))
thread2=Thread(target=worker,args=("thread 2",))
thread3=Thread(target=worker,args=("thread 3",))
thread4=Thread(target=worker,args=("thread 4",))
thread1.start()
thread2.start()
thread3.start()
thread4.start()This should be what you were looking for. I made 4 threads and I also noticed that you were writing "\n" into the file so I assumed you wanted them all to be written in a row on new lines so I fixed that for you. RE: Multi-threading Help? - Eclipse - 01-24-2015 (01-24-2015, 06:55 PM)blackhatcat Wrote: -snip- Would you recommend multi-threading or multiprocessing? Like, would implementing the above code be better or just opening the OP multiple times. (Am I getting this right?) RE: Multi-threading Help? - blackhatcat - 01-24-2015 (01-24-2015, 07:42 PM)Eclipse Wrote: Would you recommend multi-threading or multiprocessing? Like, would implementing the above code be better or just opening the OP multiple times. (Am I getting this right?) Spawning processes is a bit slower than spawning threads. Once they are running, there is not much difference. You're welcome to look up the pros and cons of both though and see which one you like more. Personally, I usually go with multithreading because it's what I'm used to and gets the job done. So I feel like it comes down to preference, most people will want multithreading but multiprocessing can work just as well. RE: Multi-threading Help? - Eclipse - 01-24-2015 (01-24-2015, 08:03 PM)blackhatcat Wrote: Spawning processes is a bit slower than spawning threads. Once they are running, there is not much difference. You're welcome to look up the pros and cons of both though and see which one you like more. I've read that multithreading is a little difficult in Python, because of the way the interpreter works. Something to do with the global interpreter lock. RE: Multi-threading Help? - Arthur Curry - 01-24-2015 (01-24-2015, 08:26 PM)Eclipse Wrote: I've read that multithreading is a little difficult in Python, because of the way the interpreter works. Something to do with the global interpreter lock. If you're worried about spawning multiple native threads then yes, the GIL will be a problem. You can easily go around this by just using Jython, however note that you'll require proper mutexing which isn't present in your current program. |