More than two years ago, in two separate episodes just four months apart, two men wielding knives in San Francisco were shot and killed by police officers. The killings spurred calls for police reform and eventually led to the resignation of the city’s police chief.

On Thursday, the San Francisco District Attorney’s Office, which investigated the episodes, announced that it would not bring criminal charges against the officers involved in either shooting.

Mario Woods, 26, was killed in December 2015. Luis Góngora Pat, 45, was killed in April 2016. In each case, the district attorney’s office determined, it was not unreasonable for the officers to have been in fear for their lives, or for the lives of others.

“Whether or not the officer could have used another tactic such as nonlethal force, or simply waiting, is not a factor we can even consider under current law,” District Attorney George Gascón said in a statement.

But at a news conference on Thursday, he said he was “extremely disturbed by the state of the law today, but yet, I am duty bound to adhere to the law.”

“To the family and to the community, there is very little that I can say to express my personal sorrow and that of my team, and nothing I can say to bring your loved ones back,” Mr. Gascón added. “I think as a society we are failing some of our most vulnerable communities when it comes to police use of force, and we are failing our police officers, too.”

Gwen Woods at a San Francisco Board of Supervisors meeting in 2016 at which a day of remembrance was proclaimed for her son, Mario Woods.CreditJim Wilson/The New York Times

On April 7, 2016, Mr. Góngora Pat was fatally shot after he was seen holding a large kitchen knife. In police reports about the episode, witnesses said he appeared to be acting aggressively and was known to be homeless. The Guardian reported that he had been sending remittances to his family in Mexico to build a house there.

In a statement on Thursday, Jeff Adachi, the San Francisco public defender, called the district attorney’s decision not to prosecute any officer in either killing “mind-boggling.”

“A hail of bullets is not an appropriate police response to people suffering mental health crises,” Mr. Adachi said. “In both the Woods and Góngora killings, officers were not in immediate danger when they fired their weapons.”

He stressed that “to date, not a single officer in San Francisco has ever been criminally charged as the result of shooting a citizen.”

On Thursday, the San Francisco Police Officers Association responded to the decision on Twitter, saying that the city’s police officers “value the sanctity of life above all else.”

“When force is used it must be reasonable and within the confines of the law,” it said. “DA George Gascon’s investigation has drawn that conclusion.”

The shootings of Mr. Woods and Mr. Góngora Pat prompted calls for police reform in San Francisco. In February 2016, the United States Justice Department announced that it would conduct a review of the San Francisco Police Department at the request of Mayor Edwin M. Lee. In April 2016, Chief Suhr released pages of racist text messages that had been exchanged among three officers in his department and said that all officers on the force would undergo anti-bias training.

But Mayor Lee asked him to resign the next month after an officer fatally shot a black woman, adding that Chief Suhr’s reforms “hadn’t been fast enough.”

In October 2016, the Justice Department announced the results of its review. It criticized the police for bias against black residents in its use of force, but stopped short of saying it was the result of racism.

William Scott, a black deputy chief from Los Angeles, was chosen to lead the San Francisco Police Department in December 2016. Mayor Lee died in December 2017.

In a statement on Thursday, Mayor Mark E. Farrell said he respected the district attorney’s decision and also acknowledged “the pain it will cause in communities that have for so long been disproportionately impacted by violence.”

“Our city’s commitment to police reform continues,” he added. “This work is more important today than ever.”