Reputation: 211
How can i configure VScode to debugg multithreaded application like this one as it is done in winpdb (we can see the different clients threads in the call stack) ?
Is it possible to see both my main Thread execution (where we are in the thread, the variables, ...), along with the thread execution (same things in the same time and execute the thread independently with the debugger console) ?
from socket import *
from fib import fib
from threading import Thread
from concurrent.futures import ProcessPoolExecutor as Pool
pool = Pool(4)
def fib_server(address):
sock = socket(AF_INET, SOCK_STREAM)
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
sock.bind(address)
sock.listen(5)
while True:
client, addr = sock.accept()
print("Connection", addr)
Thread(target=fib_handler, args=(client,), daemon=True).start()
def fib_handler(client):
while True:
req = client.recv(100)
if not req:
break
n = int(req)
future = pool.submit(fib, n)
result = future.result()
resp = str(result).encode('ascii') + b'\n'
client.send(resp)
print("Closed")
fib_server(('',25000))
Upvotes: 0
Views: 1224
Reputation: 9437
We can use breakpoints for multi-threaded debugging.
I use a simple example to show you how to do it:
from threading import Thread
from multiprocessing import Process
import time
def print_num(num):
while 1:
time.sleep(1)
print(num)
def multithread_func():
num = 2
a = [
num,
]
thread = Thread(target=print_num, args=(a,))
thread.start()
while 1:
time.sleep(1)
a[0] = a[0] + 1
def another_process(num):
while 1:
time.sleep(1)
print(f"I'm the second, num is {num}.")
if __name__ == "__main__":
from multiprocessing import Process
process = [
Process(target=multithread_func),
Process(target=another_process, args=(2,)),
]
[p.start() for p in process]
[p.join() for p in process]
We can add breakpoint before time.sleep (1):
Then, when using the debugging function, you can view the status of each thread including variables and the buttons controlling threads in the left.
Upvotes: 1