我正在尝试与主线程并行运行计时器
关于该计划:
我想从服务器向客户端发送一个映像。图像通过UDP套接字以数据包的形式发送到客户端。 定时器的作用是控制变速箱。 如果发送数据包所用的时间大于10毫秒,计时器将停止,因此数据包将重新发送。例如,假设在第100次数据包传输期间,故意延迟1秒以检查程序,此时,计时器应重置(因为10ms是阈值),并且应重新发送数据包
我曾经
multirocesssing.Process(target=countdown),
process = []
p1.start
将启动该进程
process.append(p1)
将p1
进程添加到列表中
为了在流程之间进行通信,我将flag用作全局变量,也用作流程值
flag = multiprocessing.Value()
你能帮忙吗
我使用多处理模块来实现这个目标
附加代码
def countdown(flag):
process_ID = os.getpid() #This module in Python will get the process ID when called.
print(f"Process ID: {process_ID}")
system_time = time.time()
timeout = system_time + 1
while True:
if flag.Value == 1:
break
if (time.time() > timeout) and (flag.Value == 0):
flag.Value = 1
break
def make_packet(outgoing_file, file_size): # The make packet function takes two arguments;
those two arguments are also specified by the client.
global clientAddress, p1
flag = multiprocessing.Value('i', 1)
packet = outgoing_file.read(1024) # Making the first packet by 1024 bytes.
num_of_try = 0 # Number of tries to send the total number of packets 100%
packet_num = 0
fake_checksum = hashlib.md5(packet).hexdigest() # Fake checksum to make errors intentionally.
processes = []
while packet_num != file_size:
p1 = multiprocessing.Process(target=countdown, args=(flag,))
print("-----------------------------------------------------------------------------------------")
if packet_num % 2 == 0:
seq_num = '0' # Current Sequence number.
else:
seq_num = '1' # Next Sequence number.
check_sum = hashlib.md5(packet).hexdigest() # Calculating the checksum value using hashlib
rdt_send(seq_num, packet, check_sum) # Preparing seq_num, packet and checksum for transmission.
flag.Value = 0 #Start the timer
processes.append(p1)
p1.start()
#
if num_of_try == 554:
print("MESS UP") #Creating a delay of 1second to deliberately make TIMEOUT.
time.sleep(1)
received_ack, clientAddress = serverSocket.recvfrom(1024) # Receiving the request from the client throuh serverSocket and moving it to the holder "message".
received_ack = received_ack.decode() # Decoding the response from the client.
print("Received ack - " + received_ack) # Printing the received ack
checksum_of_received_ack = hashlib.md5(received_ack.encode('utf-8')).hexdigest() # Calculating the checksum of the received ACK.
print("CALCULATED Checksum of the received ACK - " + str(checksum_of_received_ack))
received_checksum_from_client, clientAddress = serverSocket.recvfrom(1024) # Receiving checksum of the ACK from the client
received_checksum_from_client = received_checksum_from_client.decode() # Decoding the checksum received from the client.
received_checksum_from_client = str(received_checksum_from_client) # Converting the received checksum from
print("Received checksum of the ACK - " + received_checksum_from_client)
# print("Flag is " + str(flag))
if flag.Value == 1: #If timeout happens, then flag is set to 1
print("flag became 1 - Timeout happened")
dupe_ack = received_ack # Receiving the request from the client through serverSocket and moving it to the holder "message".
dupe_checksum = received_checksum_from_client
continue
if checksum_of_received_ack == received_checksum_from_client: # Comparing 'calculated checksum of the ACK' and the 'received checksum of the ACK from the client'.
print("Entered first condition")
if received_ack == seq_num: # If the checksums match, then comparing 'received ack' and 'sequence number generated'
print("Entered second condition")
flag.Value = 1 #STOPPING THE TIMER
# p1.join()
next_packet = outgoing_file.read(1024) # The next packet (1024 bytes) is read.
packet = next_packet # After receiving the positive Acknowledge, the next packet becomes the current packet.
packet_num = packet_num + 1
# print("GOOD ACK, FLAG IS " + str(flag))
#flag.Value = 0 #RESTARTING THE TIMER
# else:
# flag.Value = 1 #BAD ACK, THEN STOP THE TIMER.
# p1.join()
# flag = 0 #THEN RESTART THE TIMER.
# else:
# flag.Value = 1 #BAD ACK, THEN STOP THE TIMER.
# t1.join()
num_of_try = num_of_try + 1 # Incrementing the number of try
print("Packet number is : " + str(packet_num))
print("number of try - " + str(num_of_try))
# while packet_num != 0:
# p1.join()
outgoing_file.close() # Outgoing_file is closed.
****ERRORS I RECEIVED****
这就是我每次编译程序时收到的错误
File "C:\Users\Siyal\Phase_3\Phase_4_Server.py", line 13, in <module>
serverSocket.bind((serverName, serverPort)) # Binding the IP and Port.
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
Traceback (most recent call last):
File "<string>", line 1, in <module>
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\Siyal\Python 3.7.3\lib\multiprocessing\spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "C:\Users\Siyal\Python 3.7.3\lib\multiprocessing\spawn.py", line 114, in _main
prepare(preparation_data)
File "C:\Users\Siyal\Python 3.7.3\lib\multiprocessing\spawn.py", line 225, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Users\Siyal\Python 3.7.3\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
run_name="__mp_main__")
File "C:\Users\Siyal\Python 3.7.3\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Users\Siyal\Python 3.7.3\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Users\Siyal\Python 3.7.3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\Siyal\Phase_3\Phase_4_Server.py", line 13, in <module>
serverSocket.bind((serverName, serverPort)) # Binding the IP and Port.
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
File "C:\Users\Siyal\Python 3.7.3\lib\multiprocessing\spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "C:\Users\Siyal\Python 3.7.3\lib\multiprocessing\spawn.py", line 114, in _main
prepare(preparation_data)
File "C:\Users\Siyal\Python 3.7.3\lib\multiprocessing\spawn.py", line 225, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Users\Siyal\Python 3.7.3\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
run_name="__mp_main__")
File "C:\Users\Siyal\Python 3.7.3\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Users\Siyal\Python 3.7.3\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Users\Siyal\Python 3.7.3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
目前没有回答
相关问题 更多 >
编程相关推荐