Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I increase variable value when multithreading in python

Tags:

python

I am trying to make a webscraper with multithreading to make it faster. I want to make the value increase every execution. but sometimes the value is skipping or repeating on itself.

import threading
num = 0

def scan():
    while True:
        global num
        num += 1
        print(num)
        open('logs.txt','a').write(str(f'{num}\n'))

for x in range(500):
    threading.Thread(target=scan).start()

Result:

2
2
5
5
7
8
10
10
12
13
13
13
16
17
19
19
22
23
24
25
26
28
29
29
31
32
33
34

Expected result:

1
2
3
4
5
6
7
8
9
10
like image 352
bayuxz123 Avatar asked Jun 13 '26 20:06

bayuxz123


1 Answers

so since the variable num is a shared resource, you need to put a lock on it. This is done as follows:

num_lock = threading.Lock()

Everytime you want to update the shared variable, you need your thread to first acquire the lock. Once the lock is acquired, only that thread will have access to update the value of num, and no other thread will be able to do so while the current thread has acquired the lock.

Ensure that you use wait or a try-finally block while doing this, to guarantee that the lock will be released even if the current thread fails to update the shared variable.

Something like this:

num_lock.acquire()
try:
        num+=1
finally:
   num_lock.release()

using with:

 with num_lock:
   num+=1
like image 186
schezfaz Avatar answered Jun 17 '26 23:06

schezfaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!