China is moving rapidly to seal off its capital of 22 million residents by canceling flights and trains amid growing fears that a potential second wave of the novel coronavirus is gathering strength in Beijing and could spread to the rest of the country.

Authorities on Wednesday canceled 1,200 flights in and out of Beijing’s two airports and sharply reduced long-distance bus services. Beijingers were banned from making nonessential trips outside the city and required to take nucleic tests before they leave, municipal officials said at a briefing where they announced 31 new cases of the coronavirus.

While Beijing’s case numbers are low by global standards, they are vexing Chinese authorities who had touted their success in quickly stamping out the coronavirus and had taken pains to ensure it would not infect the seat of the ruling Communist Party. Government officials announced “wartime” measures on Saturday after dozens of cases were discovered at the Xinfadi wholesale market in southwest Beijing, which carried worrying echoes of the original outbreak in Wuhan late last year.

In the days since, Beijing has steadily reintroduced stricter measures, potentially foreshadowing the difficulty that many governments could face in warding off future waves of the virus.

After classes resumed just a month ago, Beijing’s schools were again shut on Wednesday, as were some gyms, swimming pools and public attractions. The city raised its emergency response to the second-highest level, and entry and exit checkpoints and temperature checks returned to neighborhoods in the city center. The scenes were reminiscent of those during the outbreak’s peak in February and March, albeit with less-strict lockdown measures.

The risk of people in Beijing traveling in and out of the city and spreading infections countrywide is “severe,” acknowledged Chen Bei, Beijing’s deputy party chief. “We need to take decisive action . . . resolutely control this gathering epidemic and resolutely curb its spread inside and outside the city,” Chen said.

Officials said Beijing has tested about 356,000 people since Saturday, underscoring the extent of their concern.

Adding to a sense of frustration with the virus’s reemergence, Chinese experts said it may have been lurking undetected for weeks.

“The Beijing outbreak very likely didn’t start in June, or May, but a month before that,” Gao Fu, head of the Chinese Center for Disease Control and Prevention, told reporters in Shanghai. “There can only be so many cases around us now if there were already many people with no symptoms or light symptoms back then. This is our current speculation, but we need to verify it.”

Cai Qi, the Communist Party Secretary of Beijing, told a gathering of party officials that the outbreak “has truly rung an alarm bell for us,” the Associated Press reported.

Chinese researchers say they are examining how the coronavirus made a sudden comeback. Fears that shipped livestock and produce could carry the virus — with potentially worrisome implications for international trade — began circulating this week after it was found at the Xinfadi market on cutting boards used for salmon, which is imported from Europe. The genetic material of viruses found at the market and in patients in Beijing also matched sequences typically found in patients in Europe.

Although the theory was fanned this week by nationalist media outlets including the party-run Global Times — which seized the opportunity to question whether the entire pandemic originated last year in Europe instead of China — Chinese and international experts, including the U.S. Food and Drug Administration, say there is relatively scant evidence that the virus is transmitted on food and packaging.

Wu Zunyou, the Chinese CDC’s top epidemiologist, said Tuesday that the virus was also found throughout the hall of the Xinfadi market, suggesting that the salmon was not necessarily the source. The strain of the virus found in Xinfadi is also prevalent in the United States, Wu added.

Michael Ryan, executive director of the World Health Organization’s Health Emergencies Program, also played down concerns about shipped food as a viral vector, saying the matter requires more study. “I’d be reticent to be in the position where all packaging and other things would need to be systematically tested,” Ryan said. “I don’t think it’s the primary hypothesis, but it needs to be explored.”

Meanwhile, supermarkets and restaurants in Beijing are not taking chances and have stopped selling salmon. The Beijing government said Wednesday it would carry out nucleic testing on all close contacts of people who visited the Xinfadi market, as well as workers in the city’s restaurants, which could number hundreds of thousands of people.

Cases in Beijing are expected to keep rising in the coming days because Xinfadi, which spans an area the size of 250 football fields, is such a large and central part of Beijing’s food distribution network, city officials warned.

As the capital fell again into quasi-lockdown this week, other Chinese cities, including Shanghai, began issuing quarantine requirements and restrictions for travelers from Beijing — a sudden reversal for residents who in normal times enjoy a privileged status.

Officials and state media have called for understanding and cooperation from Beijing residents while rallying citizens nationwide behind the city.

“Go Beijing! Go Zhajiang noodles!” said one popular meme on social media, referring to a classic Beijing dish.

Correction: An earlier version of this article misstated the number of coronavirus tests carried out in Beijing since June 13. It is about 356,000.