RIO DE JANEIRO — Gunfights have raged daily in several neighborhoods of Rio de Janeiro, as drug gangs battled over territory in recent weeks.

Residents have been lining up for hours in hopes of scoring a yellow fever vaccine amid an outbreak that has killed more than 20 people in the state.

But that didn’t stop thousands of people from pouring onto the streets on Friday as the city’s famed Carnival began, displaying Rio de Janeiro’s mesmerizing ability to keep chaos and carnage from derailing a party.