He was three months into his job as press attache for the US Embassy in Tehran when Barry Rosen was introduced to the Iranian Revolution.

“We were invaded,” Rosen said of a terrifying Feb. 14, 1979, attack that was a precursor to the infamous hostage crisis nine months later. “The guerillas were firing on the embassy.

“I thought they were going to execute us,” he recalled of the 100 armed interlopers. “It was like watching a film.”

In the immediate aftermath, half of his colleagues fled the country in fear. But he chose to stay in Tehran, even though his wife, Barbara — back home with their two young children in New York City — wanted him to leave.

“The place was exploding all the time, [but] I was committed to being there,” Rosen told The Post. “I thought that something dangerous would happen. I just didn’t know what.”

On Nov. 4 — Monday is the 40th anniversary — Rosen became one of the 52 Americans captured by revolutionaries in what would become known as the Iran hostage crisis. They were held for 444 days — beaten, tortured, starved and used as political pawns by extremists who had just overthrown the US-backed monarchy led by Shah Mohammad Reza Pahlavi and formed an Islamic government led by the Ayatollah Khomeini.

It was 10 a.m. on a rainy Sunday, the first day of the work week in the Muslim nation, and Rosen heard a mob chanting “Death to America” near the main gate.

He turned toward the gate and saw “a couple hundred” people,” he remembered, with pictures of Khomeini pinned to their chests, running toward the building.

“Lock the doors and hold on tight,” he told his secretary.

The mob stormed past the security officers, including US Marines. “They had weapons — guns, clubs,” Rosen said. “They lined us up against the wall. They cut the phone lines and marched me out blindfolded.”

He recalled many of his fellow hostages weeping and pleading: “I have a family” and “I don’t want to die.”

“There was panic and fear,” he said. “It was dangerous. You didn’t know what they’re going to do at any moment.”

Rosen tried to keep a clear head as he tried to negotiate the release of his Iranian staff.

Over the next 14 months, the hostages were forced to stand before mock firing squads. Some tried to take their own lives — one slashed their wrists with a piece of glass, another banged their head against the wall until losing consciousness. They were often relocated — blindfolded, bound, shoved into vans — which instilled a deep sense of uncertainty and fear. “It’s just, ‘Can I survive that moment?’” Rosen recalled.

Kept in figurative and literal darkness, he added, “I had a strong feeling of hopelessness, helplessness and depression. I had strong desires to die. But I kept on going.”

The captors were also uneasy.

“We were treated miserably, but they were constantly afraid of us. They thought we had some way of getting out,” said Rosen. “They checked our shoes for radios, as if we were Dick Tracy.”

His lowest point, he said, was being forced to falsely confess he was a spy. He had refused several times over the first month of captivity, until guards pointed automatic weapons at his head and demanded he sign a written admission — giving him 10 seconds to do it or be executed on the spot.

“I signed it,” Rosen remembered. “I was distraught. I didn’t want to live after that.”

Back at home, Rosen’s wife Barbara was meeting with international lawmakers and even the Pope, trying to negotiate the hostages’ freedom.

On Jan. 20, 1981, the day of President Ronald Reagan’s inauguration, guards told Rosen, “You’re going home.”

His reaction? “I didn’t believe them.”

About two dozen blindfolded hostages boarded buses to Algiers and were released, but not before the captors spat in their faces.

Although they were given champagne on their flight home, said Rosen, he was still suspicious: “It didn’t sink in for a long time that we were free.”

And reuniting with his family after 14 months was not straightforward.

“It took a long time to get into a rhythm of life,” he said. His daughter had been an infant when he last saw her; now, she was walking and talking. “I was a stranger to the children. I was a stranger to my wife.”

Thirty-five of the hostages are still alive and occasionally get together. On Monday, Upper West Sider Rosen will attend a State Department reception with two others.

Rosen met one of his captors, Abbas Abdi, in 1998, and said the man apologized.

“It’s too long ago to carry all that,” Rosen said, adding that the pain can never be erased. “There will never be closure . . . I internalize a lot of things, a lot of fear. It might be better to scream, but I don’t.”

So far, the hostages have collectively received two payouts totalling around $750,000, according to a source close to the Department of Justice, which administers compensation from fines collected from sanctions.

The DOJ recently announced a third payout of $1.075 billion collectively (for various hostage and terrorism victims including the Iran hostages) to be distributed sometime after Jan. 1, an agency representative confirmed.

Rosen, who is retired from a career in public affairs at various colleges, notes that today, many people change the subject when he reveals he was an Iran hostage.

“People really don’t want to know because they don’t want to feel anything,” he said. “It makes them uncomfortable. People are bored by it — what’s the next thing?”