import threadingimport timeimport sysimport os
def kenny(num=0):if num > 3:# print("Kenny dies now...")# raise SystemExit #Kenny will die, but Cartman will live forever# sys.exit(1) #Same as above
print("Kenny dies and also kills Cartman!")os._exit(1)while True:print("Kenny lives: {0}".format(num))time.sleep(1)num += 1kenny(num)
def cartman():i = 0while True:print("Cartman lives: {0}".format(i))i += 1time.sleep(1)
if __name__ == '__main__':daemon_kenny = threading.Thread(name='kenny', target=kenny)daemon_cartman = threading.Thread(name='cartman', target=cartman)daemon_kenny.setDaemon(True)daemon_cartman.setDaemon(True)
daemon_kenny.start()daemon_cartman.start()daemon_kenny.join()daemon_cartman.join()
## My example:if "ATG" in my_DNA:## <Do something & proceed...>else:print("Start codon is missing! Check your DNA sequence!")exit() ## as most folks said above
后来,我发现直接抛出一个错误会更简洁:
## My example revised:if "ATG" in my_DNA:## <Do something & proceed...>else:raise ValueError("Start codon is missing! Check your DNA sequence!")
immediateExit = Falsestart_date = '1994.01.01'end_date = '1994.01.04'resumedDate = end_date
end_date_in_working_days = Falsewhile not end_date_in_working_days:try:end_day_position = working_days.index(end_date)
end_date_in_working_days = Trueexcept ValueError: # try statement from end_date in workdays checkprint(current_date_and_time())end_date = input('>> {} is not in the list of working days. Change the date (YYYY.MM.DD): '.format(end_date))print('New end date: ', end_date, '\n')continue
csv_filename = 'test.csv'csv_headers = 'date,rate,brand\n' # not real headers, this is just for exampletry:with open(csv_filename, 'r') as file:print('***\nOld file {} found. Resuming the file by re-processing the last date lines.\nThey shall be deleted and re-processed.\n***\n'.format(csv_filename))last_line = file.readlines()[-1]start_date = last_line.split(',')[0] # assigning the start date to be the last like date.resumedDate = start_date
if last_line == csv_headers:passelif start_date not in working_days:print('***\n\n{} file might be corrupted. Erase or edit the file to continue.\n***'.format(csv_filename))immediateExit = Truesys.exit('CSV file corrupted 0.')else:start_date = last_line.split(',')[0] # assigning the start date to be the last like date.print('\nLast date:', start_date)file.seek(0) # setting the cursor at the beginnning of the filelines = file.readlines() # reading the file contents into a listcount = 0 # nr. of lines with last datefor line in lines: #cycling through the lines of the fileif line.split(',')[0] == start_date: # cycle for counting the lines with last date in it.count = count + 1if immediateExit:sys.exit('CSV file corrupted 1.')for iter in range(count): # removing the lines with last datelines.pop()print('\n{} lines removed from date: {} in {} file'.format(count, start_date, csv_filename))
if immediateExit:sys.exit('CSV file corrupted 1.2.')with open(csv_filename, 'w') as file:print('\nFile', csv_filename, 'open for writing')file.writelines(lines)
print('\nRemoving', count, 'lines from', csv_filename)
fileExists = True
except:if immediateExit:sys.exit('CSV file corrupted 1.5.')with open(csv_filename, 'w') as file:file.write(csv_headers)fileExists = Falseif immediateExit:sys.exit('CSV file corrupted 2.')
import os
# This can be called even in process worker and will kill# whole application included correlated processes as wellos.system(f"pkill -f {os.path.basename(__file__)}")